class A{
private void sayA(){
System.out.println(\"Private method of A\");
}
public static void main(String args[]){
A instanceA=new B();
ins
It's because you're assigning B to A, so the resulting instance has access to the methods of A.
If you change to B instanceA=new B(), the subsequent line will not compile (which I believe is what you expected?).
class A{
private void sayA(){
System.out.println("Private method of A");
}
public static void main(String args[]){
B instanceA=new B();
instanceA.sayA(); # This line won't compile/run.
}
}
class B extends A{
}