class Movie {
public void play() {
System.out.println("playing movie");
}
}
class DVD extends Movie {
@Override
public void play() {
System.out.println("playing dvd");
}
public void menu() {
System.out.println("showing menu");
}
}
public class Main {
public static void main(String[] args) {
Movie m = new DVD();
m.play();
m.menu(); //error
}
}
From my understanding, m is a DVD object. It prints out "playing DVD" because of that. But why
m.menu()
gives me an error if it's not overridden from the parent class Movie? It's complaining that I don't have menu() method in Movie class.
Also, in which circumstances you would declare some object as
Parent obj = new children();
like this? Do you do this when you want to make sure methods in the child class is overridden from parent? If that's the case why don't you just make the method in the parent class "abstract" or even make the parent as an Interface? My title might be misleading but is this behavior called upcasting? or polymorphism?j