In my code two classes are there ,subclass extends superclass.In sub class i override superclass method. On the time of object creation i created superclass reference to sub class object its working fine. But again i converted super class reference to complete super class object but it calls sub class methodn not super class method. My assumption the output is wrong.Here my code
public class OverRiding {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Super class reference to sub class Object
Dog dog = new Animal();
dog.eat();
dog.bow();
//Completely converted into pure Dog class Object
Dog d = (Dog) dog;
d.eat();
d.bow();
}
}
Dog class
class Dog {
public void eat() {
System.out.println("Dog eat Biscuits");
}
public void bow() {
System.out.println("Dog bow");
}
}
Class Animal
class Animal extends Dog {
public void eat() {
System.out.println("Animal eat Food");
}
}
And my output is
Animal eat Food
Dog bow
Animal eat Food
Dog bow
class Animal extends Dog? hmm I thought vice versa