HI,
i am having three
java files in two different packages.
package one;
public class One
{
protected int i=10;
protected void method1(){}
}
package two;
public class Two extends One
{
public void method2()
{
method1(); // accessed thru inheritance bcoz base class and subclass are in different packages
}
}
package two;
public class Three extends Two
{
public void method3()
{
Three tree=new Three();
tree.method1(); //shouldn't compile bcoz it tries to access base class protected members.
}
public static void main(
String args[])
{
Three tree=new Three();
tree.method3();
}
}
In the above example class Three shouldn't compile bcoz its available in different package(two) and accessing protected member of the class One available in the another package(one). But is compiling fine and works well .once a class(Two) outstide the pacakage(two) inherits a class(One) in different package(one) the protected members of the base class(One) becomes private to the subclass(Two) . but in this case class which extends that subclass is able to access the protected members of the base class is it Correct?
i think the Enacapsulation policy is violated?
can any one explain me ?