Possible Duplicate:
What does Base b2 = new Child(); signify?
I'm a Java beginner. I understand the concepts of class inheritance, but there's one thing I don't quite understand. I'm reading through Java for Dummies, and it's explaining polymorphism. It gives this code as an example:
class Player {
public void move() {...
class BetterPlayer extends Player {
public void move() {...
public class TicTacToeApp {
public static void main(String[] args) {
Player p1 = new Player();
Player p2 = new BetterPlayer();
playTheGame(p1, p2);
}
public static void playTheGame(Player p1, Player p2) {
p1.move();
p2.move();
}
}
Why was p2 created as a Player object? Here's my understanding:
If p2 were a BetterPlayer object (declared like this: BetterPlayer p2 = new BetterPlayer...):
-upcasting is automatic, so it would still work with the playTheGame method
-p2 can be used by any methods requiring a BetterPlayer object
But since it was created as a Player object, now any time p2 is used by a method requiring a BetterPlayer object, it must be explicitly cast to a BetterPlayer object, right? That seems like more work with no benefit, so I'm guessing there has to be some benefit to doing it that way; what is it?
The variable p2 was not created as a Player object. It's a reference that points to a BetterPlayer object.
Regardless whether p2 is declared as a Player or BetterPlayer reference, it can be used as a Player reference.
An advantage of declaring it as a Player is that you are prevented from coupling any code that works only with BetterPlayer, keeping open the option of using the code for another subclass option in the future -- e.g., BestPlayer.
A pretty typical analogy is a system which draws shapes.
You create many different shapes and need to place them on your screen. Writing code to do this without polymorphism is tricky. If all the specific shapes (square, circle, triangle, glu-teapot, whatever) then you can put them in a single array of type Shape and then you can just say something like
The book declared p2 as a Player only for illustrative purposes. It could be declared a BetterPlayer and the program would perform exactly the same. they wanted to show that a BetterPlayer could be assigned to a variable of type Player.
The benefit is that you are able to take advantage of all of the functions / attributes of the parent class in addition to the child class. It's a way of making your code more flexible and as you learn more about the need to create families of objects you'll begin to appreciate the added flexibility provided. For instance, suppose I have a parent class with 5 methods in it, I could create a sub-class or child and add more functionality to the class giving you access to say another 3 more specialized methods. The basic idea is that the child classes should extend the functionality of the parent class enabling you to reuse more code and keep each class shorter and more cohesive. If you want to gain an appreciation for the art of object oriented programming I recommend you take a peek at the GRASP Patterns wiki. It has sufficiently simple code examples that should help you see the value in having classes extend other classes.