Polymorphism in Java

Polymorphism in Java :

 Polymorphism is a feature of object-oriented programming that allows us to perform the same operation in multiple ways.

Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms.

Polymorphism in Java
Polymorphism in Java


There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding.

For example, lets say we have a class Vehicle that has a method vehicleColor(), here we cannot give implementation to this method as we do not know which Car class would extend Vehicle class. So, we make this method abstract like this

public abstract class Vehicle
{
public abstract void vehicleColor();
}


Now suppose we have two  classes Car1 and Car2 that extends Vehicle class. We can provide the implementation detail there


public class Car1 extends Vehicle
{
@Override
public void vehicleColor()
{
System.out.println("Red");
}
}


and another class

public class Car2 extends Vehicle
{
@Override
public void vehicleColor()
{
System.out.println("Black");
}
}


As you can see, even while vehicleColor() was a common operation for all subclasses, there were several different ways to do the same task. This is an example of polymorphism in action (feature that allows us to perform a single action in different ways).

Types of Polymorphism
Types of Polymorphism


Types of Polymorphism :

  • Static Polymorphism (compile-time)
  • Dynamic Polymorphism (runtime)

 

1.Static Polymorphism ( Compile time Polymorphism)  :  

Polymorphism that is resolved during compiler time is known as static polymorphism As an example of static polymorphism, method overloading can be considered.

Method Overloading: This allows us to have many methods with the same name but different signatures in the same class.

Example:
class TestOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
public class ExampleOverloading
{
public static void main(String args[])
{
TestOverloading obj = new TestOverloading();
obj.disp('p');
obj.disp('p',30);
}
}

Output :
p
p 30


2.Dynamic Polymorphism ( Run-time Polymorphism)  :  

Runtime polymorphism refers to a process in which a call to an  Method Overriding is resolved at runtime rather than at compile time. It is also known as Dynamic Method Dispatch.

Method Overriding : Providing new implementation to a method with same name and same signature this is known as method overriding.

In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.


Example :
class Animal
{
public void animalSound()
{
System.out.println("Default Sound");
}
}
public class Dog extends Animal
{

public void animalSound()
{
System.out.println("Woof");
}
public static void main(String args[])
{
Animal obj = new Dog();
obj.animalSound();
}
}

Output :
Woof


Upcasting: 
    If the reference variable of Parent class refers to the object of Child class, it is known as upcasting. For example:
Upcasting in java
Upcasting

class A
{
...
}
class B extends A
{
...
}

//upcasting
A a=new B();


Tech Smart

Hello, my name is Sam and I am the blog's author. I enjoy blogging since I am passionate about it and I am an enthusiastic learner. Blogging provides me a greater platform to share all of my knowledge and experiences with my readers.

Post a Comment

if you have any doubts, Please let me know

Previous Post Next Post