Parent class
class Animal {
public void sound() {
System.out.println("Grr");
}
}
Child class
class Dog extends Animal {
public void sound() {
System.out.println("Woof");
}
}
Calling class
class Callings {
public static void main(String args[]) {
Animal a = new Dog();
a.sound();
}
}
Output is - "Woof"
My concern is--> I have read till now that if we use the parent class reference and point to object to child class then using the reference of parent class we can only access the data members and methods of the parent class only, if this is true then output of above code should be "Grr". But the output is "Woof" can any one explain why.