I am reading Java in NutShell and came across Data Hiding. And than I tried few example and got confused in 1 of the example.
Suppose we have this two classes:
Class Parent{
public int a=1;
}
Class Child extends Parent {
public int a=2;
}
And Creating object of this two class in main fuction:
public static void main (String args[])
{
Child ch= new Child();
Parent pa=new Child();
System.out.println(ch.a);
System.out.println(pa.a);
}
And the output is: 2 and 1 I dont understand why second line is printing 1. Since pa is the object of Child, it should be hiding the value of parent class.
Can anyone please explain how this works?