(Question previously asked here. I did not quite get the desired answer.)
A quote from a book:
What if you want to call a method that’s defined by a subclass from an object that’s referenced by a variable of the superclass? Suppose that the SoftBall class has a method named riseBall that isn’t defined by the Ball class. How can you call it from a Ball variable? One way to do that is to create a variable of the sub- class and then use an assignment statement to cast the object:
Ball b = new SoftBall();
SoftBall s = (SoftBall)b;
// cast the Ball to a
// SoftBall
s.riseBall();
In the code snippet above, it shows a new Softball object being created and assigned as a reference to the variable b, which is completely legal since the class Softball is a subclass of the class Ball. Yet the quote states (indirectly) that you have to cast the variable to type Softball before you can use the variable to call methods from the Softball class. Why is that? Why can't I directly use the variable b of type Ball (which contains the reference to the Softball object) to call the desired method? The variable b already has the object.
(Note: I already read this post.)