top of page

Polymorphism in Java

Polymorphism Concept


Polymorphism, a Greek word, where poly means a lot and metamorphosis means change, refers to the ability of an object to assume many forms.


The concept of polymorphism is often expressed as "an interface, several methods". When a general class can have the generic method and the derived classes (classes that extend the general class) they can add the class-specific implementation to that method. At the time of execution "An interface", that is, the general class will have "many forms", that is, references of the derived classes (see the example for clarity).


Note that the polymorphism is widely used with inheritance, where the methods of the main class are replaced by the daughter classes.


Polymorphism in Java


There are two types of polymorphism in java -


• Compilation time polymorphism (or static polymorphism)

• Run-time polymorphism (or dynamic polymorphism)


We can make polymorphism in java by


• Method overload (compile-time polymorphism) - In the method overload it is known at compile time itself what overloaded method will be called.


• Replace the method (runtime polymorphism) - In the substitution of the method, it is solved in the runtime, which will be called the substituted method. The decision is based on the reference of the object for which the reference variable is pointing.


polymorphism
polymorphism


We are going to see an example of runtime polymorphism in Java to understand the concept of polymorphism.


Here we have a parent Shape class with a method area (), the Shape class has two secondary classes: Square and Circle.


In Square class, area() method is overridden and it provides the implementation to calculate area of a square.

Same way area() method is overridden in Circle class to provide implementation to calculate area of a circle.


// Super Class

class Shape{

protected double length;

Shape(double length){

this.length = length;

}

void area(){

}

}


// Child class

class Square extends Shape{

//constructor to initialize length

Square(double side){

super(side); // calling the super class constructor

}

//Overriding the area() method

void area(){

System.out.println("In area method of square");

System.out.println("Area of square - " + length*length);

}

}

// Child class

class Circle extends Shape{

//constructor to initialize length

Circle(double radius){

super(radius); // calling the super class constructor

}

//Overriding the area() method

void area(){

System.out.println("In area method of circle");

System.out.println("Area of cirlce - " + 22/7*length*length);;

}

}

public class PolymorphicTest {

public static void main(String[] args){

Shape shape;

Square square = new Square(5.0);

Circle circle = new Circle(5.0);

// shape dynamically bound to the Square object referenced by square

shape = square;

// area method of the square called

shape.area();

// shape dynamically bound to the Circle object referenced by circle

shape = circle;

// area method of the circle called

shape.area();

}

}


Notice that in PolymorphicTest class' main method object shape of class Shape is declared and later that shape object takes the reference of square and then circle and calls the area method of the respective classes.


Output after running this program would be -

In area method of square

Area of square - 25.0

In area method of circle

Area of cirlce - 75.0


So it can be seen that the shape object holds the reference of the square object initially thus the area method of the Square class is called. Later the shape object holds the reference of the Circleobject thus the area method of the Circle class is called.


Points to be observed:


• Polymorphism, a Greek word, where poly means a lot and metamorphosis means change, therefore, refers to the ability of an object to assume many forms.


• Any number of classes can implement an interface and each class is free to provide its own implementation. Thus, using interfaces, Java fully uses the polymorphism aspect "an interface, several methods".


• There are two types of polymorphism in Java, polymorphism at compile time and polymorphism at runtime.


• Method overload is an example of compile-time polymorphism in Java.


• The overriding method is an example of runtime polymorphism in Java.



 
 
 

Recent Posts

See All
From Java 8 to Java 11

Switching from Java 8 to Java 11 is more complicated than most updates. Here are some of my notes on the process. Modules Java 9...

 
 
 

Comments


SUBSCRIBE
  • Facebook Social Icon
  • Google+ Social Icon
  • Twitter Social Icon
  • LinkedIn Social Icon
LOCATION

Besant Technologies - Velachery Branch
Plot No. 119, No.8, 11th Main road, Vijaya nagar,
Velachery, Chennai - 600 042
Tamil Nadu, India
Landmark - Reliance Digital Opposite Street

+91-996 252 8293 / 996 252 8294

OPENING HOURS

© 2023 by Prickles & Co. Proudly created with Wix.com

bottom of page