I've read in the Kathy sierra book that when a subclass outside outside the package inherits the protected data member of the super class then the inherited member becomes private in the subclass so that no other class can access it using either inheritance or reference.I've written some code which contradicts the same.please go through the code and lemme know if its true or maybe I've gone wrong somewhere.
THIS IS THE SUPERCLASS IN ROHAN PACKAGE
package rohan ;
public class SuperClass
{
protected int x ;
}
THIS IS SUBCLASS OUTSIDE THE ROHAN PACKAGE IE DEFAULT PACKAGE
import rohan.SuperClass ;
class SubClass extends SuperClass
{
public static void main(
String[] args)
{
SubClass sc = new SubClass() ;
System.out.println(sc.x) ;
}
}
now as per the book the variable x has become private in SubClass and so is only accessible in subclass but look at the following code.
SimpleClass is class in default package but should not know anything about x since it is private
class SimpleClass extends SubClass
{
public static void main(String[] args)
{
SimpleClass sc = new SimpleClass() ;
System.out.println(sc.x) ;
}
}
but this code produces output 0 as if the member is still protected