• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about protected modifier

 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
gaurav singhal
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah gaurav is right
 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
10x guys
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic