• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Protected and default access of a class member

 
Roel De Nijs
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let me try to summarize all the rules concerning these 2 access modifiers in my own words and see where i go wrong (we make one assumption: the class of the member is visible to the other ones, because else this make no sense):
- protected: if a member is protected, it's accessible to all classes in the same package (doesn't matter of it is a subclass or not) and also to the subclasses and their subclasses of this class in other packages (by inheritance, so you can't access in subclass code the protected member via a reference of the baseclass). so with an example:



- default: only accessible for classes in the same packages, also it doesn't matter if it are subclasses or not

Is this correct? where did i go wrong? is there something missing that i didn't mention?

Thanks for your help!
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this excerpts from the java tutorial should dispel any lingering doubts.



and the table is



[ October 16, 2005: Message edited by: Akshay Kiran ]
[ October 16, 2005: Message edited by: Akshay Kiran ]
 
Roel De Nijs
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tested the above code out and it get some points:

- it's strange, but i tought the main-method isn't a part of the class itself and so you can't access private members from the main-method (even if its declared in the class itself). but that's a mistake, because it really can!

- this whole example isn't that good, because it has in each class one main method where it's accessing some members. but mostly �ou have some classes and then 1 class with the main-method trying to access some members of classes.

i �ade another example covering the same, but from another point of view (i am just using methods because variables and methods are members and there is no difference in access



That's it. Hope it helps others that are, like me, struggling with the member access modifiers
[ October 16, 2005: Message edited by: Roel De Nijs ]
 
Thomas Drew
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel this is wrong.


protected: if a member is protected, it's accessible to all classes in the same package (doesn't matter of it is a subclass or not)


for a class to access a protected member of another class it has to be a child of that class. Even if two class are in the same package, if class B isn't inherited from class A class B will not be able to access any protected members of class A.
 
Roel De Nijs
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Drew:
Roel this is wrong.



for a class to access a protected member of another class it has to be a child of that class. Even if two class are in the same package, if class B isn't inherited from class A class B will not be able to access any protected members of class A.



Thomas,

are you really sure about that one, because i did some tests and like you could see in my examples i posted, the class Gamma is in the same package as Alpha, but doesn't inherit of Alpha, and it can access the protected method of alpha using dot operator (it's logical that it can't by inheritance because they aren't in the same hierarchical tree)
[ October 16, 2005: Message edited by: Roel De Nijs ]
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes what thomas said is correct ....

protected members are accessible for the subclasses only whether they are in same package or not...and not to all the classes in that package ...

thanks
sri
 
Sandeep Murthy
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You are all messed up.

Any class within the same package can access protected members (variables/methods).
Sub class or not, doesn't matter. It can access protected members.

Then what's the use of protected ?
---------------------------------

Protected comes into picture for classes in OTHER PACKAGE.

If there is a subclass in other package, it can access Protected members. Not directly, buy by declaring objects of subclass, it can access protected members of parent class.

This mechanism provides a way to access protected members from other packages. Without this, only PUBLIC members will be available to classes of other packages, inherited or not.
 
Roel De Nijs
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the K&B-book:

default access = package access = all classes (subclass or not) can access a default member of a class

protected access = package + kids = all classes (subclass or not) can access a protected member of a class + all subclasses of this class in other packages could also access a protected member of a class (but it's through inheritance and not by using a dot operator on an instance of that class)

take a look at this example (subclass and other package):
package three;
import two.AlphaTwo;
public class AlphaTwoChild extends AlphaTwo {
public void printFromInheritance() {
// privateAlphaMethod(); // Compile Error
// packageAlphaMethod(); // Compile Error
protectedAlphaMethod();
publicAlphaMethod();
}

public void printUsingDotOperator() {
AlphaTwo a2 = new AlphaTwo();
// a2.privateAlphaMethod(); // Compile Error
// a2.packageAlphaMethod(); // Compile Error
// a2.protectedAlphaMethod(); // Compile Error
a2.publicAlphaMethod();
}
}
 
hoang vo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sandeep Narasimhamurthy:
Hi,

You are all messed up.

Any class within the same package can access protected members (variables/methods).
Sub class or not, doesn't matter. It can access protected members.

Then what's the use of protected ?
---------------------------------

Protected comes into picture for classes in OTHER PACKAGE.

If there is a subclass in other package, it can access Protected members. Not directly, buy by declaring objects of subclass, it can access protected members of parent class.

This mechanism provides a way to access protected members from other packages. Without this, only PUBLIC members will be available to classes of other packages, inherited or not.


Sandeep and Roel are right in my opinions.

Public - Any class can access in any package. (least restrictive)

Protected - Any inherited class IN or OUT of the package AND any class IN the same package. (more restrictive)

default(no modifier) - Any class IN the same package. (more more restrictive)

Private - Within the class (most restrictive)
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the same package only subclasses can access the protected member not all classes...
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srikanth,

In the same package, all classes can access protected memebers of other classes.
 
Marcin Skrzek
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only right answer is: protected members are accessible from every class in the same package!!!

I have compiled the following example code, so it must be true.

//"One.java"
package first;
public class One {
protected int i=5;
}

//"Two.java"
package first;
public class Two {
public static void main(String...args) {
One one = new One();
System.out.println(one.i); //OK: Two doesn`t extends One,
//but both clases are in the
//same package,
//so Two has access to One`s
// i protected instant variable
}
}

package second;
import first.*;
public class Three extends One{
public void main(String...args) {
int j = i; //OK: i is inherited from One
//and i is protected, so it`s available here

One one = new One();
//j = one.i; //compile error i has
//protected access in first.One
}
}
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLS
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes seb...
for same package..
protected memebers can be accessed by the subclasses of the same package ...
and for other classes of the same package it can be accessed through the dot operator...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic