File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Protected Access Confusion 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 Access Confusion" Watch "Protected Access Confusion" New topic
Author

Protected Access Confusion

Jerry Ragland
Ranch Hand

Joined: Apr 26, 2005
Posts: 33
I have 3 files A.java, B.java and C.java all the 3 java files are in the same directory.

A.java -



B.java -



C.java -




Compiled all the 3 files as below -



Now I have 2 class files A.class and B.class package abc and another set of 2 files C.class and D.class in packages xyz.

I excepted class D will throw an error(but it didn't) when it tries to invoke protected go() method of class A as D is in a separate package and it doesn't inherit the go() method directly from A, rather it does it through C.

To my understanding a protected method when inherited from a different package will get inherited as private in C. Then how could D inherit a private member from C and calls it.

Please help me in understand this.

Thanks in Advance.
-Jerry.

(No shouting in topic titles, please)
[ February 26, 2007: Message edited by: Barry Gaunt ]
Raji Rama
Greenhorn

Joined: Jan 16, 2007
Posts: 5
To my understanding a protected method when inherited from a different package will get inherited as private in C. Then how could D inherit a private member from C and calls it.


the protected method will be private to C.but,it can be accessed by subclasses of C.since d is subclass of C,no problem.
But it cannot be accessed by other classes in the package "xyz".
[ February 26, 2007: Message edited by: Raji Rama ]
Jerry Ragland
Ranch Hand

Joined: Apr 26, 2005
Posts: 33
Originally posted by Raji Rama:

the protected method will be private to C.but,it can be accessed by subclasses of C.since d is subclass of C,no problem.
But it cannot be accessed by other classes in the package "xyz".

[ February 26, 2007: Message edited by: Raji Rama ]



I am afraid Raji, I don't understand how a method which is private to a class can be accessed by its subclass. The private method of the super class will not be even visible to subclass.

-Jerry
Bunty Naagar
Greenhorn

Joined: Feb 27, 2007
Posts: 14
As K&B puts it "Once the subclass outside the package inherits the protected member, that member becomes private to any code outside the subclass, with the exception of subclasses of the subclass"
[ February 27, 2007: Message edited by: Bunty Naagar ]

SCJP 5, SCWCD 5
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
We must remember the factors behind protected access:

1-You make a class member variable and methods protected if you want to make it accessible outside the package too BUT only through inheritance. Protected means package + kids. Kids are the subclasses that may be outside the package of the class in which the protected member is defined.
2-Outside the package, you can�t access protected member of a class using the reference of the class in which it is defined. This statement needs good clarification. OK! So let�s give you a good example to get insight of the concept

package certification;

public class A {
protected int x=10;
}


package scjp;
import certification.A;

public class B extends A{

void display(){
//First way to access protected member x of class A in package certification
System.out.println(�x = � + x);
//Second way to access protected member x of class A in package certification
B b1 = new B();
System.out.println(�x = � + b1.x);

//and what can�t be done, means restricted
A a1 = new A();
System.out.println(�x = � + A1.x);//compiler error

}
}


/* MORE CLARIFICATION*/
package scjp;

public class C extends B {
void display() {
//First way to access the protected member of class B
System.out.println(�x = � + x);
//Second way to access the protected member of class B
C c1 = new C();
System.out.println(�x = � + c1.x);

//What causes compilation error, or restricted to do
B b1 = new B();
System.out.println(�x = � + b1.x); //NO, compiler error
A a1 = new A();
System.out.println(�x = � + a1.x);//NO, compiler error
}
}


Conclusion: Protected member of a class in other package can only be accessed by the subclass in another package directly as if it would member of the same class; in other words without using dot �.�operator, or using the reference of self, that extends the parent class. You see that, class C can�t acccess the member x using reference of the class B but inside class B the protected member x of class A of package certification is easily accessed. It directly means that for the extending class the protected member remains protected for the next inheriting classes but private for others which are not subclasses. Here private for others means, if there is another class in package �scjp� that is standalone means does not extend the class B or so. If it tries to access the protected (inherited from A) member x, it fails to access because it is private to outside world except the inheriting classes.


[ February 28, 2007: Message edited by: Chandra Bhatt ]
[ February 28, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Hi Chandra..Thanks for the pretty descriptive explanation..!!
But, to me -- there is a correction thr....
In package SCJP..
instead of class B-- should it not be Class B extends class A..
I think ..only when class B extends A, your explanation will hold true..
Please clarify..
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected Access Confusion
 
Similar Threads
using the javac -cp command
Import
packages imported correctly but the source files are not getting compiled well from K & B chapter 10
Package
Intriguing....Oops basics defined.