A small presentation on Polymorphism in Java that I made for college in my BSc 2nd semester.
P.S.- I am an indie developer, check out my apps and games :)
1. Polymorphism in Java
Name: Animesh Sarkar
Subject: CMSA
Semester: 2
Paper: CC3
Roll no.: S49
Session: 2020-2023
1
2. Index
2
Topic name Page no.
What is Polymorphism? 3
A graphical example of polymorphism in pop culture. 4
Types of polymorphism. 5
Static polymorphism. 6
Example program to illustrate method overloading. 7, 8
Dynamic polymorphism. 9
Example program to illustrate method overriding, 10, 11
Differences between method overloading and overriding. 12
Some other polymorphic behaviours. 13, 14
A program illustrating various kinds of polymorphism. 15, 16, 17
Advantages and disadvantages of polymorphism. 18
Reference sources. 19
3. What is polymorphism?
● Polymorphism refers to the ability to appear in many
forms. It is the concept of one entity providing multiple
implementations or behaviours.
● It is an important feature of Object-Oriented
Programming. Java supports polymorphism.
3
4. Henry Cavill Superman
Clerk Kent
Geralt
One person shows different characteristics and
behaviours depending on the character he plays.
This is an example of polymorphism.
4
5. Java has two types of polymorphism
Compiled
or static
polymorphism
Runtime
or dynamic
polymorphism
5
6. Compiled or static polymorphism
This type of polymorphism is achieved by method overloading and
operator overloading. However, Java does not support user defined
operator overloading.
Method overloading
When there are multiple methods with same name but different
parameter list then these methods are said to be overloaded. Methods
can be overloaded by changing number of arguments and/or change in
type of arguments.
6
7. Example program to illustrate method overloading
public class Adder
{
public static int add (int a, int b)
{
System.out.println(“Add 2 int”);
return a+b;
}
public static int add (int a, int b, int c)
{
System.out.println(“Add 3 int”);
return a+b+c;
}
public static double add (double a, double b)
{
System.out.println(“Add 2 double”);
return a+b;
}
Method 1
Method 2
Method 3
The class Adder
provides multiple
implementations
of the add
method (or
function).
7
8. Example program to illustrate method overloading
public static void main (String[] args)
{
add (5, 7);
add (3, 6, 9);
add (2.4, 4.5);
}
}
Method 1 will be called
At each function
call, the compiler
determines
which add
method to call
based on the
data types and
number of
arguments
passed.
8
Method 2 will be called
Method 3 will be called
Add 2 int
Add 3 int
Add 2 double
Output
9. Runtime or dynamic polymorphism
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
Method overriding occurs when a derived class has a definition for one
of the member methods of the base class. That base class function is
said to be overridden by the child class function.
9
10. Example program to illustrate method overriding
class Pizza
{
protected int radius = 16;
protected double rate = 0.8;
public double price ()
{
System.out.println(“Price of pizza”);
return (rate * Math.PI * Math.pow (radius, 2));
}
}
class HalfPizza extends Pizza
{
public double price ()
{
System.out.println(“Price of half pizza”);
return (rate * 0.5 * Math.PI * Math.pow (radius, 2));
}
}
Base class
Derived class
The base class
and the derived
class both
provide their
own
implementations
of the price
method.
10
11. Example program to illustrate method overriding
public class Price
{
public static void main (String[] args)
{
Pizza pz = new Pizza();
pz.price();
pz = new HalfPizza();
pz.price();
}
}
When the method price is called on
an object of the base class,
normally the base class method is
called. But when the same object is
initialised with a derived class
constructor, the derived class
method overrides the base class
method and so the derived class
method is called. This cannot be
determined at compile but during
runtime.
11
Price of pizza
Price of half pizza
Output
12. Differences between method overloading and
method overriding
12
Method overloading Method overriding
Increases the readability of the program by limiting
the need for unique method names.
Provides the specific implementation of the method
that is already provided by its super class.
Occurs within a class.
Occurs in two classes that have inheritance
relationship.
Parameter must be different. Parameter must be same.
Compile time polymorphism. Runtime polymorphism.
Return type can be same or different. Return type must be same or covariant.
13. There are other characteristics in the Java programming
language that exhibit polymorphism.
➔ Coercion
Polymorphic coercion deals with implicit type conversion done by the compiler to
prevent type errors. Some typical examples are -
double db = 9.7 + 8.1f + 2; // db = 19.8
String sos = “Help-” + 0.2; // sos = “Help-0.2”
➔ Operator overloading
Java supports some pre-defined overloaded operators. For example, the addition
operator is overloaded. It is used for both addition and string concatenation. Like -
int mx = 98 + 25; // mx = 123
String txt = “In” + “ “ + (1 + 2) + ‘D’; // txt = “In 3D”
13
14. There are other characteristics in the Java programming
language that exhibit polymorphism.
➔ Constructor overloading
Sometimes, we need multiple constructors to initialize different values of a class. We
can easily overload constructors like methods. For example -
public class Cat {
private int age;
public Cat () { age = 0; } // Constructor 1
public Cat (int age) { this.age = age; } // Constructor 2
public static void main (String[] args) {
Cat kit = new Cat (); // Constructor 1 is called
Cat kat = new Cat (3); // Countructor 2 is called
}
}
14
15. A program illustrating various kinds of polymorphism
interface Sides {
int countSides ();
}
abstract class Polygon {
double length;
abstract double perimeter ();
abstract double area ();
}
class Square extends Polygon implements Sides {
public int countSides() {
return 4;
}
double perimeter () {
return length * countSides();
}
double area () {
return length * length;
}
The class Square inherits from an
abstract class and also an
interface.
The abstract methods have
to be overridden by the
derived concrete class.
Coercion happens in some
of the methods.
The class Square
inherits from the
abstract class Polygon
and the interface
Sides. So the derived
class has two parent
classes.
This is somewhat like
multiple inheritance.
Java does not directly
support multiple
inheritance.
15
16. A program illustrating various kinds of polymorphism
void print () {
System.out.println ("Name of polygon = Square");
System.out.println ("Number of sides = " + countSides());
System.out.println ("Length = " + length);
System.out.println ("Perimeter = " + perimeter());
System.out.println ("Area = " + area());
}
}
public class GreenSquare extends Square {
void print () {
System.out.println ("Color = Green");
super.print ();
}
void print (String message) {
System.out.println (message);
}
public GreenSquare () {
length = 0 + 1;
}
The class GreenSquare is
a concrete class that
inherits from Square,
which is another another
concrete class.
The method print has been
overloaded.
The class also has
overloaded constructors.
The overloaded
+ operator has
been used for
addition of
integers as well
as for string
concatenation.
The method
print has been
both overridden
and overloaded.
16
17. A program illustrating various kinds of polymorphism
public GreenSquare (int length) {
this.length = length;
}
public static void main (String args[]) {
Square shape = new GreenSquare (6);
shape.print ();
}
}
All these
different kinds
of polymorphism
provides a lot of
stability and
flexibility.
This example
shows how
polymorphism is
important for
supporting
inheritance.
17
The reference type for
the object shape is
Square while its actual
type is GreenSquare.
The print method
defined in the
derived class
GreenSquare will be
called, instead of the
one defined in
Square.This is
because of method
overriding.
Output
Color = Green
Name of polygon = Square
Number of sides = 4
Length = 8.0
Perimeter = 32.0
Area = 64.0
18. 18
Advantages of Polymorphism
● Polymorphism helps in reducing the coupling between different functionalities.
This increases the modularity of the program.
● It increases reusability which improves maintainability and scalability.
● Ensures the proper implementation of inheritance through method overriding.
● The concept of polymorphism is more aligned with the real world.This allows for
more robust design solutions.
● Makes programming more intuitive.
Disadvantages of Polymorphism
● Increases design complications and makes debugging harder.
● Reduced readability because the program’s runtime actions has to be recognised.
● Dynamic polymorphism may lead to performance issues.
19. Reference Sources
● https://www.geeksforgeeks.org/polymorphism-in-java
● https://www.mygreatlearning.com/blog/polymorphism-in-java
● https://www.slideshare.net/SpotleAI/polymorphism-in-java-149138166
● https://www.javatpoint.com/constructor-overloading-in-java
● https://www.javatpoint.com/method-overloading-vs-method-overriding-in-java
● https://teachcomputerscience.com/polymorphism
● https://www.baeldung.com/java-interface-vs-abstract-class
● Images have been taken from various websites through Google Images.
19