AppDividend
Latest Code Tutorials

Polymorphism in Java: Step by Step Guide

Polymorphism comes from two Greek words poly means many, and morph means shape or type. So Polymorphism can be defined as a word that can be displayed in different/many forms. But in the case of Java, it is an operator or the constructor or a method that can be shown in many forms.

What is Polymorphism in programming

Polymorphism is one of the OOPs concepts. Inheritance lets us inherit the attributes and methods from another class. 

For example, you have a cellphone for communication, the communication mode you choose could be anything like call, a text message, picture message, an mail, etc. So, our main goal is common, that is, communication, but their approach is different. This is called Polymorphism.

Polymorphism in Java

Polymorphism has the capability of the method to do different things based on an object that it is acting upon. In other words, Polymorphism allows you to define one interface and have multiple implementations. Polymorphism in Java uses those methods to perform different tasks. Polymorphism allows us to perform a single action in different ways.

There are mainly two types of Polymorphism in Java:

  1. Compile-time Polymorphism
  2. Run time polymorphism

Compile-time Polymorphism

Any operation which shows Polymorphism during compile time is known as Compile time polymorphism. It is also known as static Polymorphism. For example, operator overloading, constructor overloading, and method overloading are compile-time polymorphism examples.

Operator overloading

Java gives accessibility to overload operators. For example, we can use the ‘+’operator for the addition of two numbers and the concatenation of two strings. ‘+’ is the only operator in Java used for operator overloading.

Constructor overloading

When a class has more than one constructor with different parameters, it is known as constructor overloading. This shows Polymorphism. See the following program.

class child {
  child() {
    System.out.println("Hi !");
  }

  child(String name) {
    System.out.println("Name is " + name);
  }

  child(String name, int std) {
    System.out.println("My name is " + name + "and I'm in class" + std);
  }

  public static void main(String args[]) {
    child ch = new child();
    child ch1 = new child("Shouvik");
    child ch2 = new child("Shouvik ", 10);
  }
}

See the following output.

Polymorphism in Java

Method overloading

Method overloading means a class has multiple methods with the same name but different in the parameter. And this method of overloading comes under compile-time Polymorphism.

See the following program.

class overload {
  void show(String name) {
    System.out.println(name);
  }

  void show(String name, int std) {
    System.out.println(name + " " + std);
  }
}

class goal {
  public static void main(String[] args) {
    overload ob = new overload();
    ob.show("shouvik");
    ob.show("shouvik", 10);
  }
}

See the following output.

Method overloading in Java

Run time polymorphism

Any operation which shows Polymorphism during run time is known as Run time polymorphism. It is also known as Dynamic Polymorphism. Method overriding is the best example of run time polymorphism.

Upcasting

If a reference variable of the Parent class refers to an object of the Child class, it is known as upcasting.

See the following syntax.

class A{}  
class B extends A{}
A a=new B();

We can use a reference variable of the class type or an interface type for upcasting.

interface I{}  
class A{}  
class B extends A implements I{}

Now, let’s define the relationships.

B IS-A A
B IS-A I
B IS-A Object

Method overriding

Method overriding in Java can be defined as a method in a subclass or child class already present in the superclass.

See the following program.

class car {
  void run() {
    System.out.println("car is running");
  }
}

class bike extends car {
  void run() {
    System.out.println("bike is running");
  }

  public static void main(String args[]) {
    bike ob = new bike();
    ob.run();
  }
}

See the following output.

Method overriding

Java Runtime Polymorphism with data member

A method is overridden, not the data members, so data members can’t achieve runtime polymorphism.

In the example given below, both the classes have the data member speed. We are accessing a data member by a reference variable of the Parent class, which refers to a subclass object. Since we are accessing a data member who is not overridden, it will always access a data member of the Parent class.

See the following code example.

class Car {
    int speed = 80;
}

class GC extends Car {
  int speed = 140;

  public static void main(String args[]) {
    Car c = new GC();
    System.out.println(c.speed);
  }
}

See the following output.

Java Runtime Polymorphism with Data Member

Java Runtime Polymorphism with Multilevel Inheritance

Let’s see the simple example of Runtime Polymorphism with multilevel inheritance.

class Doc {
  void print() {
    System.out.println("Printing");
  }
}

class PDF extends Doc {
  void print() {
    System.out.println("Printing PDF Docs");
  }
}

class Word extends Doc {
  void print() {
    System.out.println("Printing Word Docs");
  }

  public static void main(String args[]) {
    Doc d1, d2, d3;
    d1 = new Doc();
    d2 = new PDF();
    d3 = new Word();
    d1.print();
    d2.print();
    d3.print();
  }
}

See the following output.

Java Runtime Polymorphism with Multilevel Inheritance

Difference between Static and Dynamic Polymorphism

Static Polymorphism
Dynamic Polymorphism
It relates to the method overloading. It relates to a method overriding.

Errors, if any, are resolved at a compile-time. Since our code is not executed during compilation, the name is static.

Ex:

void mul (int x , int y);
void mul (float x, double y);
int mul (int x, int y); //compiler gives error.

If the reference variable is calling an overridden method, a method to be invoked is determined by an object your reference variable is pointing to. It can only be decided when the code is under execution at the runtime, hence the name dynamic.

Ex:

//reference of parent pointing to child object
 Engineer eng = new ITE();
// method of child called
eng.createProduct();

 

That’s it for this tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.