Please check the following code
package Inheritance;
public class TestInheritance1 {
public static void main(
String[] args)
{
A A1 = new A();
B B1 = new B();
A1 = B1;
A1.valueInt();
B1.valueInt();
System.out.println("........."+A1.i);
System.out.println("........."+B1.i);
}
}
class A
{
int i=0;
public void valueInt()
{
System.out.println("Value of i from class A:" + i);
}
}
class B extends A
{
int i = 5;
public void valueInt()
{
System.out.println("Value of i from class B :" + i);
}
}
Its giving the o/p as:
Value of i from class B :5
Value of i from class B :5
.........0
.........5
When we have assigned A1=B1, then why A1.i and A1.valueInt() are giving different values?