I'm trying to understand the object reference created in SuperClass and SubClass. I read a lot online but still not able to figure out which reference is called. Here is my code.
public class X {
public void xCall() {
System.out.println("X method called");
}
public void move(){
System.out.println("I'm in parent class");
}
}
public class Y extends X{
public void yCall() {
System.out.println("Y method called");
}
public void move(){
System.out.println("I'm in child class");
}
}
I tried to making following objects and got the result as shown.
X a = new X();
X b = new Y();
a.move();// runs the method in X class
b.move();//Runs the method in Y class
Output :
I'm in parent class
I'm in child class
My question is why
b.move();
is giving output
`I'm in child class`
instead of
`I'm in parent class`
My question is different from the one marked as duplicate of. As I understand that it will refer the superclass not the subclass. My question is then why is it giving me the result of method in the subclass?