Explain the advantages of polymorphism.


Teaching Note:

For example, an action in a child object may choose to override actions of a parent object. This allows an external program to use the same action on a family of objects without knowing the implementation detail.


Sample Question:


JSR Notes:


Note that here the same method name would be used in different levels of inheritance. So the parent might have a toString() method which prints out its few attributes, but one of its sub-classes can also have the same toString() method, which it over-writes, and in doing so, it would print out all the other attributes it has too. See the example:

public class Car{
    private String name = "not set yet";
private String color = "black"; public Car(String name, String color){ this.name = name;
this.color = color; } //gets and sets... public String toString(){ return"All the car details are: " + name + " " + color + "."; } }
Now we will see "over-writing" polymorphism by over-writing the toString() method (which, in fact was over-writtin in Car, since it comes from the Object class - noting that all classes extend the Object class, even though "extends Object" does not have to be written).
public class SportsCar{
    private boolean hasSpoiler = false;
    public SportsCar(String name, String color, boolean hasSpoiler){
        super(name, color);
        this.hasSpoiler = hasSpoiler;

    //gets and sets...
    public String toString(){
        return "All the sports car details are: " + name + " " + color + "that it has a spoiler is " + hasSpoiler + ".";


So now we have two general examples of polymorphism: overloading, and over-writing:

Overloading - We generally think of this as overloaded constructors. Two or more constructors (so exactly the same name of method) with different parameter lists - different numbers and/or orders of parameters. Look back at D.2.3.

You know it's "overloading" because there are two or more methods in the same class with the same name (but different parameter lists).


Overriding - Where one child sub-class totally overrides a method found above it in the inheritance chain. So the same exact spelling and in fact same order and number of parameters can have different results depending on which class it is called from.

You know it's "overriding" when it's a method with the same name as another method in one of the classes it inherits from. (And it can have the same or different parameter lists; it's generally more what it does than the parameter list which is overridden.


The Advantage of Using Polymorphism

In a sentence, the advantage is that you can keep the same name for similar, yet slightly different methods.

So with the overloaded constructor situation, all constructors for a certain class can be called the same thing - the name of the class - even though they operate differently because of the different list of parameters.

And in the case of overriding of inherited methods, such as toString(), the same name can be used for similar, yet slightly different, methods.  By keeping consistency of naming in this way, other programers can get a hint as to the functionality of a method by recognizing the name. The assumption can be made that the overridden methods with names we recognize to have certain functionality based on the familiar name.

Interestingly, this is similar to the way that we prefer to use the same name for both parameters and the internal attributes that they get assigned to. We would prefer to call, for example, a student's name studentName, whether it be the name being received as a parameter, or the attribute it gets assigned to. "studenName" is the perfect identifier, so why change it for one of those two variables. (And you'll recall we solve the potential mixing up of the two with the "this." clarification.) So it's kind of the same with "toString" for example. toString is a method to give a String representation of an object, so when coming up with a specific representation of an object of a particular class, why make a method by some other name, "toString" is perfect. And all programmers familiar with Java will know what to expect from it, through the particular implementation.

If fact, a bit more about polymorphism will help reinforce this desire to have consistent terms and so on. Using operators in various ways, depending on the kind of operand(s) being worked with, is, in fact, another example of polymorphism. The + operator in-between two integers works one way (arithmetically adding them together), but another way in-between two strings (it concatinates them together). These are different, yet similar uses, so we prefer to have only one operator, in this case, used (dependent on the context).