0% found this document useful (0 votes)
8 views9 pages

EEI3262 Unit 2 Session 8

This document provides an overview of polymorphism in object-oriented programming, specifically within Java. It explains the two types of polymorphism: static (method overloading) and dynamic (method overriding), detailing how they function and their implications in programming. Additionally, it discusses late binding and includes activities for practical application of the concepts learned.

Uploaded by

udarasenu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

EEI3262 Unit 2 Session 8

This document provides an overview of polymorphism in object-oriented programming, specifically within Java. It explains the two types of polymorphism: static (method overloading) and dynamic (method overriding), detailing how they function and their implications in programming. Additionally, it discusses late binding and includes activities for practical application of the concepts learned.

Uploaded by

udarasenu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Session 8 : Polymorphism

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

8.2 Different types of polymorphism


Java supports two types of polymorphism, static or compile-time
polymorphism and dynamic polymorphism.

8.2.1 Static Polymorphism or Method Overloading


Java, like many other object-oriented programming languages, allows you to
implement multiple methods within the same class that use the same name
but a different set of parameters. That is called method overloading and
represents a static form of polymorphism. Here the behaviour is decided at
the compile time and not at the run time.

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.

float calcArea(float a, float b, float c){


// to calculate the area of a pyramid
}

float calcArea(float x, float y){


//to calculate the area of a rectangle
}

 The types of the parameters need to be different


e.g. one method accepts a String and another one a long.

void displayInfo(String name){


}

void displayInfo(long population){


}

 They need to expect the parameters in a different order.


e.g. one method accepts a String and a Long and another one accepts
a Long and a String.

void displayInfo(String name, long population){


}

void displayInfo(long population, String name){


}

97
Session 8 : Polymorphism

This kind of overloading is not recommended because it makes the API


difficult to understand which method to call. In most cases, each of these
overloaded methods provides a different but very similar functionality.

8.2.1.1 Method Signature


In Java, a method signature is part of the method declaration. It's the
combination of the method name and the parameter list.
It's the ability to write methods that have the same name but accept different
parameters. The Java compiler can separate the difference between the
methods through their method signatures.
Consider the given method Signature Examples
public void setMapReference(int xCoordinate, int
yCoordinate)
{
//method code
}
The method signature in the above example is setMapReference(int, int). In
other words, it's the method name and the parameter list of two integers.
public void setMapReference(Point position)
{
//method code
}
The Java compiler will let us add another method like the above example
because its method signature is different, setMapReference(Point) in this
case.

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).

Due to the different sets of parameters, each method has a


different signature. That allows the compiler to identify which method has
to be called and to bind it to the method call. This approach is called static
binding or static polymorphism.

98
Session 8 : Polymorphism

8.2.1.2 Implementing static polymorphism

Let’s look at an example for static polymorphism.

The BasicCoffeeMachine class implements two methods with the


name brewCoffee. The first one accepts one parameter of
type CoffeeSelection. The other method accepts two parameters,
a CoffeeSelection, and an int.

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);

If we change this code and call the brewCoffee method with


a CoffeeSelection object and an int, the compiler binds the method call to the
other brewCoffee(CoffeeSelection selection, int number) method.
BasicCoffeeMachinecoffeeMachine = createCoffeeMachine();
List coffees =
coffeeMachine.brewCoffee(CoffeeSelection.ESPRESSO, 2);

In the next section we will be studying dynamic polymorphism.

99
Session 8 : Polymorphism

8.2.2 Dynamic polymorphism


This form of polymorphism doesn’t allow the compiler to determine the
executed method. The JVM needs to do that at runtime.
Within an inheritance hierarchy, a subclass can override a method of its
superclass. That enables the developer of the subclass to customize or
completely replace the behavior of that method.
It also creates a form of polymorphism. Both methods, implemented by the
super and subclass, share the same name and parameters(signature) but
provide different functionality.

Check the example given below with dynamic polymorphism implemented.

When calling the addition method on MyCalculator instance execute the


method in the subclass. If you want to call the addition method of the super

100
Session 8 : Polymorphism

class you need to create an object of the super class. You can instantiate
MyCalculator object as given below.

Calculator calc = new MyCalculator();

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.

Activity 8.1 : Polymorphism

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

Source code is available in https://tinyurl.com/demo-TestPolymorphism-


OUSL

8.3 Late binding

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:

 Superclass referenced as the superclass


Calculator calc = new Calculator();

 Subclass referenced as the subclass


MyCalculator calc = new MyCalculator();

 Subclass referenced as the superclass


Calculator calc = new MyCalculator();

Activity 8.2: Polymorphism

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

Source code is available in


https://tinyurl.com/demoTestPolymorphismShap-OUSL

Activity 8.3: Polymorphism

Write and compile the program given below and explain how the polymorphism is
implemented. Is it static or dynamic polymorphism? Justify your answer.

Source code is available in https://tinyurl.com/demo-


TestPolymorphismAnim-OUSL

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.

Within an inheritance hierarchy, a subclass can override a method of its


superclass. If you instantiate the subclass, the JVM will always call the
overridden method, even if you cast the subclass to its superclass. That is
called dynamic polymorphism.

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.

2. Function overloading is another way to explain polymorphism. Do you


agree with this statement?

104

You might also like