Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default confusion in "package and import "statement :(

    hi guys ,

    I am in great confusion about the access specifier in package . please explain why public members alone can be accessed by the class in other package ? why not protect ? !!!!!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by funkygarzon View Post
    hi guys ,

    I am in great confusion about the access specifier in package . please explain why public members alone can be accessed by the class in other package ? why not protect ? !!!!!
    As a matter of fact a class D can access the members in class B if those members are protected; class D has to extend class B. Protected access crosses package boundaries through extension.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Access/visibility modifiers (in order of rank [highest first]):

    public — Accessible from everywhere
    protected — Only accessible from the same package as well as from subclasses
    * — Only accessible from the same package
    private — Only accessible from the same class

    * is the default access modifier and is used when no access modifier is used (i.e. in absence of the keywords: public, private, protected).
    * is called ”default” or ”package private”.


    Due to the existence of synthetic access (ugly enough to use; it is implicitly used) private methods/classes/&c can be access from inner classes,
    otherwise at least package private must be used.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  4. #4
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    As a matter of fact a class D can access the members in class B if those members are protected; class D has to extend class B. Protected access crosses package boundaries through extension.

    kind regards,

    Jos
    Quote Originally Posted by Hibernate View Post
    Access/visibility modifiers (in order of rank [highest first]):

    public — Accessible from everywhere
    protected — Only accessible from the same package as well as from subclasses
    * — Only accessible from the same package
    private — Only accessible from the same class

    * is the default access modifier and is used when no access modifier is used (i.e. in absence of the keywords: public, private, protected).
    * is called ”default” or ”package private”.


    Due to the existence of synthetic access (ugly enough to use; it is implicitly used) private methods/classes/&c can be access from inner classes,
    otherwise at least package private must be used.

    THANK YOU VERY MUCH FOR YOUR REPLY JosAH , Hibernate ,

    Hope you are all fine by god grace .

    Now coming to my doubt : practically i tried this theory by creating a class "Balance" in package called "MyPack" and i have created another class called "TestBalance" in another package called "MyPack2" but the protected members are not accessible by "TestBalance" ,so please help me and correct the error in my code below

    Java Code:
    package MyPack;
    
    public class Balance {
    	String name;
    	double bal;
    	protected  Balance(String n, double b) {
    	name = n;
    	bal = b;
    	}
    	protected  void show() {
    	if(bal<0)
    	System.out.print("--> ");
    
    	System.out.println(name + ": $" + bal);
    	}
    	}
    
     package MyPack2;
    
    import MyPack.*;
    class TestBalance  {
    	
    	public static void main(String args[]) {
    	/* Because Balance is public, you may use Balance
    	class and call its constructor. */
    	Balance test[] = new Balance[3];
    	
    	test[0] = new Balance("K. J. Fielding", 123.23);
    	test[1] = new Balance("Will Tell", 157.02);
    	test[2] = new Balance("Tom Jackson", -12.33);
    	
    	for(int i=0; i<3; i++) test[i].show();
    
    	}
    	
    	}
    Last edited by sunde887; 08-11-2011 at 12:05 PM. Reason: Switched quote tags to code tags

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default

    Class TestBalance doesn't extend class Balance so it doesn't have access to its protected members.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Class TestBalance doesn't extend class Balance so it doesn't have access to its protected members.

    kind regards,

    Jos
    Yes JosAH , i even tried like tried extending the Balance class , but still i am getting error bro :( . can you please correct my code ? please bro i am in great confusion

    package MyPack;

    public class Balance {
    String name;
    double bal;
    protected Balance(String n, double b) {
    name = n;
    bal = b;
    }
    protected void show() {
    if(bal<0)
    System.out.print("--> ");

    System.out.println(name + ": $" + bal);
    }
    }

    package MyPack2;


    class TestBalance extends Balance {

    public static void main(String args[]) {

    Balance test[] = new Balance[3];

    test[0] = new Balance("K. J. Fielding", 123.23);
    test[1] = new Balance("Will Tell", 157.02);
    test[2] = new Balance("Tom Jackson", -12.33);

    for(int i=0; i<3; i++) test[i].show();

    }

    }

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default

    What is the exact error message? And where? For one thing: I don't see any protected data members (just methods).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    What is the exact error message? And where? For one thing: I don't see any protected data members (just methods).

    kind regards,

    Jos
    ah yes bro , here only i am getting some confusion , ony protected members are allowed ? not protected methods ?

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by funkygarzon View Post
    ah yes bro , here only i am getting some confusion , ony protected members are allowed ? not protected methods ?
    I didn't write that; show us your latest code and the exact compiler message you get.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    I didn't write that; show us your latest code and the exact compiler message you get.

    kind regards,

    Jos
    java.lang.NoClassDefFoundError: Balance
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknow n Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: Balance
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 12 more
    Exception in thread "main"
    the above one is the error message i am getting in Eclipse IDE bro

  11. #11
    abhilasha is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    i think u missed the below statement
    import MyPack.Balance;

    in your TestBalance class

  12. #12
    abhilasha is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    ur TestBalance class should lokk like this

    package MyPack2;

    import MyPack.Balance;

    public class TestBalance extends Balance {

    protected TestBalance(String n, double b) {
    super(n, b);
    }

    public static void main(String args[]) {

    TestBalance test[] = new TestBalance[3];
    test[0] = new TestBalance("",123.25);
    test[1] = new TestBalance("Will Tell", 157.02);
    test[2] = new TestBalance("Tom Jackson", -12.33);

    for(int i=0; i<3; i++) test[i].show();

    }

    }

  13. #13
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by abhilasha View Post
    ur TestBalance class should lokk like this

    package MyPack2;

    import MyPack.Balance;

    public class TestBalance extends Balance {

    protected TestBalance(String n, double b) {
    super(n, b);
    }

    public static void main(String args[]) {

    TestBalance test[] = new TestBalance[3];
    test[0] = new TestBalance("",123.25);
    test[1] = new TestBalance("Will Tell", 157.02);
    test[2] = new TestBalance("Tom Jackson", -12.33);

    for(int i=0; i<3; i++) test[i].show();

    }

    }
    Thanks for the reply bro , for the above code i am getting the below error bro ,

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The constructor Balance(String, double) is not visible
    The constructor Balance(String, double) is not visible
    The constructor Balance(String, double) is not visible
    The method show() from the type Balance is not visible

    at MyPack2.TestBalance.main(TestBalance.java:14)

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by funkygarzon View Post
    Thanks for the reply bro , for the above code i am getting the below error bro ,
    Show us your code again because this is a mess; I suspect that you're still not extending from Balance in your TestBalance class; your compiler told you so but you still attempt to run the thing.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    abhilasha is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    do u see m not calling new Balance(String,Double) from main method
    instead u hv to use the sub Class ie new TestBalance(String,Double)

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by abhilasha View Post
    do u see m not calling new Balance(String,Double) from main method
    instead u hv to use the sub Class ie new TestBalance(String,Double)
    That is not needed; please don't confuse the OP even more.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Show us your code again because this is a mess; I suspect that you're still not extending from Balance in your TestBalance class; your compiler told you so but you still attempt to run the thing.

    kind regards,

    Jos




    package MyPack;

    public class Balance {
    String name;
    double bal;
    protected Balance(String n, double b) {
    name = n;
    bal = b;
    }
    protected void show() {
    if(bal<0)
    System.out.print("--> ");

    System.out.println(name + ": $" + bal);
    }
    }

    package MyPack2;

    import MyPack.Balance;
    class TestBalance extends Balance {

    protected TestBalance(String n, double b) {
    super(n, b);
    }
    public static void main(String args[]) {

    Balance test[] = new Balance[3];

    test[0] = new Balance("K. J. Fielding", 123.23);
    test[1] = new Balance("Will Tell", 157.02);
    test[2] = new Balance("Tom Jackson", -12.33);

    for(int i=0; i<3; i++) test[i].show();

    }

    }
    Quote Originally Posted by abhilasha View Post
    do u see m not calling new Balance(String,Double) from main method
    instead u hv to use the sub Class ie new TestBalance(String,Double)
    yes bro , i can understand that you have created a contractor for "TestBalance" and calling super class contractor by using the keyword "super " , that is an amazing idea bro .... but still i am getting error , please check my above code :(

  18. #18
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Do not construct, Balance, construct TestBalance:

    Java Code:
    Balance test[] = new Balance[3];
    
     test[0] = new TestBalance("K. J. Fielding", 123.23);
     test[1] = new TestBalance("Will Tell", 157.02);
     test[2] = new TestBalance("Tom Jackson", -12.33);
    OR (if you decide you need to access new methods located in TestBalance, but not in Balance.)

    Java Code:
    TestBalance test[] = new TestBalance[3];
    
     test[0] = new TestBalance("K. J. Fielding", 123.23);
     test[1] = new TestBalance("Will Tell", 157.02);
     test[2] = new TestBalance("Tom Jackson", -12.33);
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  19. #19
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Because of show being protected only the second alternative will compile.


    Edit:
    Conclusion:
    static members do never have access to protected members in other packages.

    Edit 2:
    Also:
    protected constructors are only accessible by the line:
    super(…);
    Last edited by Hibernate; 08-11-2011 at 03:14 PM.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  20. #20
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by Hibernate View Post
    Because of show being protected only the second alternative will compile.


    Edit:
    Conclusion:
    static members do never have access to protected members in other packages.

    Edit 2:
    Also:
    protected constructors are only accessible by the line:
    super();
    wooooooow you are rocking bro , now all are working without error .

    Because of show being protected only the second alternative will compile.
    protected constructors are only accessible by the line:
    super();
    can you please explain me the above two sentence in detailed manner . ? thank s in advance bro ...

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 06-16-2011, 04:44 PM
  2. An "if" statement inside a "for" loop?
    By soccermiles in forum New To Java
    Replies: 18
    Last Post: 04-20-2010, 03:44 AM
  3. Replies: 4
    Last Post: 10-30-2009, 07:54 PM
  4. Eclipse "import not resolved" package problem?
    By spamsickle in forum New To Java
    Replies: 3
    Last Post: 08-24-2009, 11:44 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •