aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Protected and default access of a class member Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Protected and default access of a class member" Watch "Protected and default access of a class member" New topic
Author

Protected and default access of a class member

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5266
    
  13

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!


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
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 ]

"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5266
    
  13

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

Joined: Sep 15, 2004
Posts: 47
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
Bartender

Joined: Jul 19, 2004
Posts: 5266
    
  13

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

Joined: Jul 28, 2005
Posts: 252
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


Thanks & Regards<br /> <br />-Srikanth
Sandeep Murthy
Ranch Hand

Joined: Oct 05, 2005
Posts: 34
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.


SCJP 1.4
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5266
    
  13

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

Joined: May 03, 2005
Posts: 14
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

Joined: Jul 28, 2005
Posts: 252
in the same package only subclasses can access the protected member not all classes...
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
Srikanth,

In the same package, all classes can access protected memebers of other classes.


Regards,<br />Seb<br /> <br />SCJP 1.4
Marcin Skrzek
Ranch Hand

Joined: Oct 19, 2005
Posts: 38
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
}
}


SCJP 5, SCWCD 1.4, SCBCD 5
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
JLS
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected and default access of a class member