File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about protected modifier" Watch "question about protected modifier" New topic

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(){

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
Welcome to javaranch

U have 2 options

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

Always use instant UBB Code in putting ur codes here.


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

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
I agree. Here's the link:
subject: question about protected modifier
Similar Threads
Access protected member from the class of different package.
protected member access
Confused on protected modifier
inheritance of protected attribute
Problem about protected attribute