Logout

D.2.3

Define the term polymorphism.

 

Teaching Note:

Actions have the same name but different parameter lists and processes.

 

Sample Question - FORMER CURRICULUM:

Define polymorphism, when used as a feature of object-oriented programming. [2 marks]

JSR Notes:

Let's start with the OOP particular application of the term, and then generalize it.

(Possibly looking ahead) we know that the main time we use polymorphism is with overloaded constructors. One time we may make a Student object with just the studentName, but another time with both studentName and grade. So there are two constructors with exactly the same name, but each has a different parameter list (in this case different numbers of parameters - one and two) - see below:


Student(String studentName)

Student(String studentName, int grade) // Same name, different parameter list

Before moving onto a general example, remember that not only do these actions (i.e. methods) have different parameter lists, but they have different processes, as seen below with the full implementation of these two overloaded constructors.

 


Student(String studentName){ this.studentName = studentName; }

Student(String studentName, int grade){
this.studentName = studentName; // Not only a different parameter list, but different actions within, as well.
this.grade = grade;
}

 

So, generally, polymorphism is what its name says "poly" meaning many, and "morphism", meaning changed form. You could say that a sports stadium exercises polymorphism; at times it is a hockey rink, but at other times (with different physical "parameters" put inside), it becomes a basketball arena, or indeed a music auditorium. All three instances of it share the common attributes of roof, main seats, bathrooms, and staircases, but each "incarnation" of it, i.e. it's different (poly) shapes (morphisms) add or re-arrange things, such as adding ice, a wooden floor, or a stage and speakers.

We could come up with myriad other examples, but that should do for here, and actually this assessment statement is more geared toward the programming, OOP, polymorphism.

 

Overloading and Overriding - both classic examples of polymorphism

The term "overloaded" is used most often in association with contstructors. The overloaded constructors are all the other constructors beyond the default constructor. There can be many overloaded constructors for any given class, each with their own distinct number and type of parameters.

Here is an example of a default constructor and two overloaded constructors:


//default constructor
public Student(){

}

//overloaded constructor # 1
public Student(String name){
        this.name = name;
}

//overloaded constructor # 2
public Student(String name, int grade){
        this.name = name;
        this.grade = grade;
}

 

Overriding is when the functionality of a method in a parent class is overridden by a child class. The most common example of this is overriding how the Object method toString() works. Programmers often override toString() to return a certain representation of the class. So you might want an object of the student class to look something like the following return String:

public String toString(){
        String s = "-------> Student Name: " + name + "\n" + "------->Student grade: " + grade;
return s;

This will give output somethign like:

-------> Student Name: John Smith
-------> Student Grade: 10