The process of specifying the same method with many implementations is known as polymorphism. That entails developing numerous ways with various behaviours.

Java uses method overloading and method overriding to provide polymorphism.

Polymorphism ad hoc

A technique called ad hoc polymorphism is used to define the same method with several implementations and arguments. Ad hoc polymorphism in the java programming language is implemented using the concept of method overloading.

The method binding in ad hoc polymorphism takes place during compilation. Compile-time polymorphism is another name for ad hoc polymorphism. According to the arguments, every function call was bound to the appropriate overloaded method.

Ad hoc polymorphism is only used within a class.

Let’s examine the Java code in the example below.

Pure polymorphism

A way for defining the same method with the same arguments but multiple implementations is called pure polymorphism. Pure polymorphism is achieved in the Java programming language using the idea of method overriding.

Run time method binding takes place in pure polymorphism. Run-time polymorphism is another name for pure polymorphism. Every function call has an object reference-based binding to the corresponding override method.

The parent class function is considered to be overridden when a child class defines a member function of the parent class.

Only the inheritance idea is implemented with pure polymorphism.

Run Time Polymorphism in JAVA

A call to an overridden method is resolved using a process known as runtime polymorphism, sometimes known as dynamic method dispatch, rather than at compile time.

In this procedure, a superclass’s reference variable is used to invoke an overridden method. The object that the reference variable is referring to determines which method should be invoked.

Prior to understanding Runtime Polymorphism, let’s first learn Upcasting. Upcasting occurs when a reference variable of the parent class refers to an object of the child class.

Real Life Examples of Polymorphism in JAVA

A person may be in various relationships with various persons. A woman can simultaneously play the roles of mother, daughter, sister, and friend, which means she exhibits other behaviours in various contexts.

There are numerous organs in the human body. The heart is in charge of blood flow, the lungs are in charge of breathing, the brain is in charge of thought, and the kidneys are in charge of excretion. So, depending on the body organ, our typical method function behaves differently.

Example of JAVA Polymorphism

class Shapes {

  public void area() {

    System.out.println(“The formula for area of “);



class Rectangle extends Shapes {

  public void area() {

    System.out.println(“Rectangle is ½ * base * height “);



class Circle extends Shapes {

  public void area() {

    System.out.println(“Circle is 3.14 * radius * radius “);



class Main {

  public static void main(String[] args) {

    Shapes myShape = new Shapes();  // Create a Shapes object

    Shapes myrectangle= new Rectangle();  // Create a Rectangle object

    Shapes myCircle = new Circle();  // Create a Circle object








The formula for the area of Rectangle is ½ * base * height

The formula for the area of the Circle is 3.14 * radius * radius.


Facebook Comments