Sorry for so much spam these days.
Okay , so I trying to understand upcasting and downcasting and I can't seem to click how come a parent class reference is able to access it's own methods and methods of the child which the child has overriden?
Like in this code ,
class Animal{
void anotherMethod(){
System.out.println("Hello in Animal");
}
void makeSound(){
System.out.println("Make sound in Animal");
}
}
class Deer extends Animal{
public Deer(){
System.out.println("Hello , in Constructor");
}
void color(){
System.out.println("The color is Red");
}
void makeSound(){
System.out.println("Deer noise");
}
}
public class Hello{
public static void main(String[] args) {
Animal animal = new Deer(); // What does this line mean?
animal.anotherMethod();
animal.makeSound();
}
}
If I have to explain , ig , a superclass is always to access methods of it self and it's subclass that the subclass has overriden
But where I am getting is how is the superclass accessing those methods?