java polymorphism tutorial Polymorphism in Java

⇒ Definition :-

Polymorphism is defined as the process of performing the same action in different ways.It is the combination of two words poly and morphism.where “poly” means

. That means a single instance can have more then one form. In java polymorphism is achieved through methods.

Real life Example of polymorphism: Water can have different forms at different place.Like solid,liquid,and gas.So the water possessing different behaviour in different situations. This is called polymorphism.

⇒ Types of Polymorphism :-

We have two types of polymorphism in java.

  1. Compile time Polymorphism
  2. Run-time Polymorphism

⇒ Compile time Polymorphism:-

This polymorphism is handled by compiler itself.It is also known as the static compiler.Compile time Polymorphism is achieved by method overloading in java.

Polymorphism in java method overloading:-

In same class we have multiple functions with same name with different arguments is called as the method overloading. That means we are overloading the same name method again again.For a method to be overloaded in java must follow any one of the condition.

 method-overloading
  1. Number of arguments should be different
  2. Sequence of arguments should be different
  3. Types of arguments should be different.

java programs on polymorphism

Different ways to achieve Polymorphism With different number arguments:

1. Number of arguments should be different

class Test

{

void show(int a)

{

System.out.println("1");

}

void show()

{

System.out.println("2");

}

public static void main(String[] args)

{

Test t = new Test();

t.show(5);// It will call first function

}

}

Output:-

1

2.With different sequence of arguments

class Test

{

void show(int a,String b)

{

System.out.println("1");

}

void show(String a,int b)

{

System.out.println("2");

}

public static void main(String[] args)

{

Test t = new Test();

t.show(1,"Ram");

t.show("Rahul",2);

}

}

Output:-

1

2

3.With different type of arguments

class Test

{

void show(int a)

{

System.out.println("1");

}

void show(String b)

{

System.out.println("2");

}

public static void main(String[] args)

{

Test t = new Test();

t.show("Ram"); // It will call the second function

}

}

Output:-

2

⇒ Run-time Polymorphism:-

Run-time polymorphism is the process of calling the overridden method through the reference variable of superclass. It is also known as the

Dynamic Method Dispatch :- 

  1. In this overridden methods are resolved at Run-time rather then the compile time.
  2. Run-time polymorphism in java is achieved by

Polymorphism in java Method-Overriding:-

It is an feature which allow to provide the specific implementation of same method in the child class that is already provided in Superclass.Means with the same name of the method in both base class and child class with same arguments we can provide different implementations. For a method to be overloaded in java must have following condition.

  1. Number of arguments should be same
  2. Sequence of arguments should be same
  3. Types of arguments should be same
  4. There must be IS-A relationship(Inheritance)

Different ways to achieve polymorphism

1.With no arguments

class Test

{

void show()

{

System.out.println("parent class function called");

}

}

class Paper extends test

{

void show()

{

System.out.println("Child class function called");

}

public static void main(String[] args)

{

Test t = new Test();

t.show(); // It will call the parent class function

Paper p = new Paper();

p.run(); // It will call the child class function

}

}

Output:-

Parent class function called

Child class function called

2.With same type of arguments

class Test

{

void show(int a)

{

System.out.println("parent class function called");

}

}

class Paper extends test

{

void show(int a)

{

System.out.println("Child class function called");

}

public static void main(String[] args)

{

Test t = new Test(1);

t.show(); // It will call the parent class function

Paper p = new Paper(2);

p.run(); // It will call the child class function

}

}

Output:-

Parent class function called

Child class function called

3.With same sequence of arguments

class Test

{

void show(String a,int b)

{

System.out.println("parent class function called");

}

}

class Paper extends test

{

void show(String a,int b)

{

System.out.println("Child class function called");

}

public static void main(String[] args)

{

Test t = new Test("coding",1);

t.show(); // It will call the parent class function

Paper p = new Paper("java",2);

p.run(); // It will call the child class function

}

}

Output:-

Parent class function called

Child class function called

Leave a Comment