wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about protected modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about protected modifier" Watch "question about protected modifier" New topic
Author

question about protected modifier

ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54
i read on a book it say that "When a subclass-outside the package inherits a protected emember, the member is private inside the sub-class".
below is the code:
package certification;
public class Parent {
protected int x=9;
}
package other;
import certification.Parent;
public class Child extends Parent{
public void testIt(){

System.out.println(x);
}
}
package test;
import other.Child;
public class aaa extends Child{
public void zzz(){

System.out.println(x); //the doubt i having is over here...
}
}

If the x in class child is private,then how can i access it in class aaa??
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Hi
Welcome to javaranch

U have 2 options

1. Increase the accessibility of i in Parent from protected to public
or
2. extend aa from Parent instead of Child


Always use instant UBB Code in putting ur codes here.


Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
gaurav singhal
Ranch Hand

Joined: Nov 18, 2005
Posts: 135
Hi Ming,

In the case of protected member the following is true.

When a subclass outside the package inherits the protected member, the member is essentially private inside the sbclass, such that only the subclass and its subclasses can access it.

I hope that clarify your doubt

Gaurav
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
yeah gaurav is right
ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54
10x guys
 
 
subject: question about protected modifier