AppDividend
Latest Code Tutorials

Polymorphism in Java Example | Java Polymorphism Tutorial

0

Polymorphism in Java Example | Java Polymorphism Tutorial is today’s topic. The word polymorphism comes from two Greek word poly means many, and morph means shape or type. So polymorphism can be defined as a word which can be displayed in different/many forms. But in case of Java, it is an operator or the constructor or a method which can be shown in many forms.

#What is polymorphism in programming

Polymorphism is a capability of the method to do the 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.

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

Polymorphism in Java

Polymorphism one of the OOPs concept.Inheritance lets us inherit the attributes and methods from another class. Polymorphism uses those methods to perform different tasks. Polymorphism allows us to perform the single action in the 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. Operator overloading, constructor overloading, and method overloading are examples of compile time polymorphism.

#Operator overloading

Java gives the accessibility to overload operator. For example, we can use ‘+’operator for the addition of two number as well as for 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 is having multiple methods with the same name but different in the parameter. And this method 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 Child class, it is known as upcasting.

See the following syntax.

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

For upcasting, we can use a reference variable of the class type or an interface type.

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 which is 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 A 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 which is not overridden, hence it will access a data member of the Parent class always.

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, hence the name static.

Ex:

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

In case, 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 be only decided at the runtime when the code is in under execution, hence the name dynamic.

Ex:

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

 

Finally, Polymorphism in Java Example | Java Polymorphism Tutorial is over.

Leave A Reply

Your email address will not be published.

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