Hello All,
Here is a Program,
package myDemo;
public class MyOverRidingDemo {
public MyOverRidingDemo() {
super();
// TODO Auto-generated constructor stub
A obj=new B();
obj.add();
System.out.println("Value Of I : "+obj.i);
}
/**
* @param args
*/
public static void main(
String[] args) {
// TODO Auto-generated method stub
new MyOverRidingDemo();
}
}
class A {
int i=10;
public void add(){
System.out.println("Add() in Class A");
}
}
class B extends A {
int i=20;
public void add(){
System.out.println("Add() in Class B");
}
}
When i run this program, the out put is as follows :
Add() in Class B
Value Of I : 10
It is pretty clear that B is overriding the add() method of Class A.
When an instance of B is made and assigned to a variable of type A,
obj.add() should refer to add of B
and obj.i should refer to i of B.
but over here..why is it Referring to i Of A???