This is known as polymorphism.
Imagine the following example:
Musician musician = new Pianist();
or
Musician musician = new Drummer();
Suppose Musician
class has a method: play()
. It doesn't matter who the musician is, if you call play method, you'll know how to play the determined instrument, base on concrete class, on this case, no matter if Pianist or Drummer.
Each concrete class, overwriting its play()
method, can play on your own:
e.g. for Pianist
class: play() { playsBeethoven(); }
For detailed information, please check http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html
It's always good to remember to use it with inheritance http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html