Given the follow code:
public class Musician {
public void play() {
// do something
}
}
.
public class Drummer extends Musician {
public void turnsDrumStick() {
// do something
}
}
.
public class Guitarist extends Musician {
public void strummingStrings() {
// do something
}
}
I can use polymorphism to do the following:
Musician m1 = new Guitarist();
Musician m2 = new Drummer();
m1 = m2;
However, I can't see the methods of the subclass:
m1.strummingStrings(); //COMPILATION ERROR!
If I use:
Guitarist m1 = new Guitarist();
Would not it be better? What is the advantage of using Musico type to reference an object of a subclass? Just a possibility I would be able to attribute m1 = m2;
, for example? Or are there other advantages?
I saw this post, but I'm still puzzled: Using superclass to initialise a subclass object java