CHAPTER 11
OOP CONCEPTS: POLYMORPHISM
Syllabus
Polymorphism concept; types: method overloading and overriding; application; polymorphism in java;
sufficient examples.
Polymorphism
The word polymorphism means having many forms.
In simple words, we can define polymorphism as the ability of a message to be displayed in more
than one form.
Real-life Illustration: Polymorphism
A person at the same time can have different characteristics. Like a man at the same time is a father,
a husband, an employee. So the same person possesses different behaviour in different situations.
This is called polymorphism.
The word “poly” means many and “morphs” means forms
Types of Polymorphism
In Java polymorphism is mainly divided into two types,
1. Compile-time Polymorphism
2. Runtime Polymorphism
Compile-time polymorphism
It is also known as static polymorphism.
This type of polymorphism is achieved by function overloading.
Runtime polymorphism
It is also known as Dynamic Method Dispatch.
It is a process in which a function call to the overridden method is resolved at Runtime.
This type of polymorphism is achieved by Method Overriding.
Method overriding, on the other hand, occurs when a derived class has a definition for one of the
member functions of the base class. That base function is said to be overridden.
Method Overloading
In Java it is possible to define two or more methods within the same class that share the same name,
as long as their parameter declarations are different.
When this is the case, the methods are said to be overloaded, and the process is referred to as
method overloading.
If a class has multiple methods having same name but different in parameters, it is known as
Method Overloading.
Method overloading is one of the ways that Java supports polymorphism.
As we can see, method( ) is overloaded three times.
Subject: SEPP Notes/20CS43P pg. 1
The first version takes no parameters, the second takes one integer parameter, the third takes two
integer parameters.
Example,
class OverloadDemo {
void method() // Overload method with no arguments
{
[Link]("No parameters");
}
void method(int a) // Overload method for one integer parameter.
{
[Link]("a: " + a);
}
void method(int a, int b) // Overload method for two integer parameters.
{
[Link]("a and b: " + a + " " + b);
}
double method(double a) // overload method for a double parameter
{
[Link]("double a: " + a);
return a*a;
}
}
class OverloadExample
{
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
[Link](); // call all versions of method()
[Link](10);
[Link](10, 20);
result = [Link](123.25);
[Link]("Result of [Link](123.25): " + result);
}
}
Demonstration of Method Overloading using Java Construct
Subject: SEPP Notes/20CS43P pg. 2
Overloading allows different methods to have the same name, but differ by the number of input parameter
or type of input parameters or both. Overloading is related to compile time polymorphism.
Public class sum {
Pubic int sum (int x, int y)
{
Return(x + y);
}
Public int sum(int x, int y, int z)
{
Return(x+y+z);
}
Public double sum(double x, double y)
{
Return (x+y);
}
Public static void main(String args[])
{
Sum s= new sum();
[Link]([Link](10,20));
[Link]([Link](10,20,30));
[Link]([Link](10.5,20.5));
}
}
Output:
30
60
31.0
Advantage of Method Overloading
Method overloading increases the readability of the program.
Different ways to overload the method
There are two ways to overload the method in java
1. By changing number of arguments
2. By changing the data type
Method Overloading: changing no. of arguments
In this example, we have created two methods, first add() method performs addition of two numbers
and second add method performs addition of three numbers.
In this example, we are creating static methods so that we don't need to create instance for calling
methods.
class Adder{
static int add(int a,int b)
{
Subject: SEPP Notes/20CS43P pg. 3
return a+b;
}
static int add(int a,int b,int c)
{
return a+b+c;
}
}
class TestOverloading1{
public static void main(String[] args){
[Link]([Link](11,11));
[Link]([Link](11,11,11));
}
}
output:
22
33
Method Overloading: changing data type of arguments
In this example, we have created two methods that differs in data type. The first add method receives
two integer arguments and second add method receives two double arguments.
class Adder{
static int add(int a, int b) {
return a+b;
}
static double add(double a, double b)
{
return a+b;
}
}
class TestOverloading2{
public static void main(String[] args)
{
[Link]([Link](11,11));
[Link]([Link](12.3,12.6));
}
}
Output:
22
24.9
Method Overriding
In a class hierarchy, when a method in a subclass has the same name and type signature as a method
in its superclass, then the method in the subclass is said to override the method in the superclass.
Subject: SEPP Notes/20CS43P pg. 4
When an overridden method is called from within a subclass, it will always refer to the version of
that method defined by the subclass.
The version of the method defined by the superclass will be hidden.
Rules for Java Method Overriding
1. The method must have the same name as in the parent class
2. The method must have the same parameter as in the parent class.
3. There must be an IS-A relationship (inheritance).
Consider the following,
class Shape
{
void draw()
{
[Link]("Inside the method of Parent class ");
[Link]("Drawing Shapes");
}
}
//Derived class
class Circle extends Shape
{
//Overriding method of base class with different implementation
@Override
void draw()
{
[Link]("Inside the overridden method of the child class ");
[Link]("Drawing Circle");
}
}
//Driver class
public class MethodOverridingDemo
{
public static void main(String args[])
{
//creating object of Base class Shape - If a Parent type reference refers
// to a Parent object, then Parent's draw() method is called
Shape obj = new Shape();
Subject: SEPP Notes/20CS43P pg. 5
[Link]();
// If a Parent type reference refers to a Child object Child's draw() method is called.
//This is called RUN TIME POLYMORPHISM.
Shape obj1=new Circle();
[Link]();
}
}
Output:
Inside the method of Parent class
Drawing Shapes
Inside the overridden method of the child class
Drawing Circle
Overriding v/s Overloading
Subject: SEPP Notes/20CS43P pg. 6
QUESTIONS
1. All concepts
Subject: SEPP Notes/20CS43P pg. 7