EEI3262 Unit 2 Session 8
EEI3262 Unit 2 Session 8
Session 8
Polymorphism
Contents
Introduction, p96
8.1 Polymorphism, p96
8.2 Different types of polymorphism, p 97
8.3 Late binding, p102
Summary, p104
Learning Outcomes, p104
Review Questions, p104
Introduction
This session gives you an insight to the object-oriented concept,
Polymorphism. Polymorphism is a requirement of any true object-oriented
programming language (OOPL). In Java polymorphism is facilitated by
overriding and overloading. After reading this session you will be learning
to apply polymorphism to process objects differently based on their data
type and class.
8.1 Polymorphism
The ability to appear in many forms is referred to as Polymorphism.
In object-oriented programming, polymorphism refers to a programming
language's ability to process objects differently depending on their data
type or class. More specifically, it is the ability to
redefine methods for derived classes. For example, given a base class shape,
polymorphism enables the programmer to define different area methods for
any number of derived classes, such as circles, rectangles and triangles. No
matter what shape an object is, applying the area method to it will return the
correct result.
96
Session 8 : Polymorphism
The parameter sets have to differ in at least one of the following three
criteria:
They need to have a different number of parameters.
e.g. one method accepts 2 and another one 3 parameters.
97
Session 8 : Polymorphism
In our last example of a Java method signature, if you follow the same rules
as the first two examples, you can see that the method signature here
is calculateAnswer(double, int, double, double).
98
Session 8 : Polymorphism
Now when you call one of these methods, the provided set of parameters
identifies the method which will be executed.
In the following code snippet, when we call the method only with
a CoffeeSelection object, at compile time, the Java compiler binds this
method call to the brewCoffee(CoffeeSelectionselection) method.
BasicCoffeeMachinecoffeeMachine = createCoffeeMachine();
coffeeMachine.brewCoffee(CoffeeSelection.FILTER_COFFEE);
99
Session 8 : Polymorphism
100
Session 8 : Polymorphism
class you need to create an object of the super class. You can instantiate
MyCalculator object as given below.
When calling calc.addition(a,b) the addition method from the sub class will
be called. The reason is JVM always calls method of the instance and not of
the reference type(which is the Calculator). With the use of Calculator as
the reference type, you will not be able to call methods specific to sub class
such as multiplication in this example. This is further discussed under the
topic late binding in the next section. If you want to call the multiplication
method by using the object refereed by calc, you can do that by casting.
You have to refer the given reference to learn casting.
Write and compile the program given below and explain how the polymorphism is
implemented. Is it static or dynamic polymorphism? Justify your answer.
101
Session 8 : Polymorphism
When you want to use an inheritance hierarchy in your project, you need to
be able to answer the following question, which method will the JVM call?
That can only be answered at runtime because it depends on the object on
which the method gets called. The type of the reference, which you can see
in your code, is irrelevant. You need to distinguish three general scenarios:
Write and compile the program given below and explain how the polymorphism is
implemented. Is it static or dynamic polymorphism? Justify your answer.
102
Session 8 : Polymorphism
Write and compile the program given below and explain how the polymorphism is
implemented. Is it static or dynamic polymorphism? Justify your answer.
103
Session 8 : Polymorphism
Summary
Polymorphism is one of the core concepts in OOP languages. It describes
the concept that different classes can be used with the same interface. Each
of these classes can provide its own implementation of the interface.
Java supports two kinds of polymorphism. You can overload a method with
different sets of parameters. This is called static polymorphism because the
compiler statically binds the method call to a specific method.
Learning Outcomes
Now you will be able to:
explain the use of Polymorphism
apply polymorphism in your programs
implement dynamic polymorphism by using late
binding
Review Questions
1. What you understand by the term Polymorphism? How does the
Inheritance support polymorphism? Give examples.
104