print
Advertisment
Advertisment

Polymorphism in java

Polymorphism is a very good feature of Object-Oriented Programming. Having many forms in one form is called polymorphism. In other words, Polymorphism is a concept in which we can do the same work in two different ways. Polymorphism The word 'poly' and 'morph' has been formed by combining these two words.

Real-Life Example for Polymorphism in java

There are many bike companies in real life. When the bike is used, then that bike has many features like speed, average, engine, self-start, and color. Here the name of the bike is the same but it has many forms (company).

class Bike{  
  int average(){
    return 0;
  }
}  
class Hero extends Bike{  
  int average(){
    return 60;
  }  
}  
class Apache extends Bike{  
  int average(){
  return 45;
  }  
}  
class Bullet extends Bike{  
  int average(){
    return 40;
  }  
}
class Sample{
  public static void main(String args[]){  
     Bike s = new Hero();  
System.out.println("Hero average every liter: " + s.average() + "km");  
  s = new Apache();  
System.out.println("Apache average every liter : " + s.average() + "km");  
  s = new Bullet();  
System.out.println("Bullet average every liter : " + s.average() + "km");  
}  
}

 

Output :

 
  
  Hero average every liter: 60km 
  Apache average every liter : 45km
  Bullet average every liter : 40km

  

Simple Example for Polymorphism in java

class Poly{
  public void method(){
    System.out.println("Parent method");
  }

  public void method(String s){
    System.out.println("Polymorphism");
    }
  }

 class Morphism extends Poly {
   public static void main(String[] args) {
  
  Morphism o = new Morphism();

       o.method();
       o.method("java topic");
   }
 }


 

Output :

 
  
  Parent method
  Polymorphism
  

Types of Polymorphism in java

Two Types of Polymorphism

  1. Compile Time Polymorphism
  2. Run Time Polymorphism

Compile Time Polymorphism in java

Compile Time Polymorphism is also called Static Polymorphism. Method overloading occurs in compile time polymorphism. In which the names of the same types of methods and different types of parameters (arguments) are available.

Example

class Poly{
  public void method(){
    System.out.println("Parent method");
  }

  public void method(String s){
    System.out.println("Polymorphism");
    }
  }

 class Morphism extends Poly {
   public static void main(String[] args) {
  
  Morphism obj = new Morphism();

       obj.method();
       obj.method("java topic");
   }
 }


 

Output :

 
  
  Parent method
  Polymorphism
  

Example

class Poly{
    public void method(int a ,int b){
      System.out.println("parent method");
    }
     
    public void method(String s,int a){
        System.out.println("Polymorphism");
      }
    }

class   Example  extends Poly {
  public static void main(String[] args) {
  Poly obj = new Example ();
       obj.method(5,6);
     obj.method("java topic",6);
  }
   }



 

Output :

 
  
  Parent method
  Polymorphism
  
Advertisment

If we use the same method name and same parameter in the compile-time argument. So compile time will throw an error.

Example

class Java{
   public void method(int a ,int b){ 
     System.out.println(" ");
  }

   public void method(int a,int b){
     System.out.println(" ");
    }
  }

class Topic extends Java {
  public static void main(String[] args) {
   Java j = new Java();

       j.method(5,6);
       j.method(4,6);
     }
   }


 

Output :

 
  

C:\Users\a\Desktop>javac Topic.java
Topic.java:9: error: method method(int,int) is already defined in class Java
        public void method(int a,int b){
                    ^
1 error
  

Run Time Polymorphism in java

Example

class Example{
       public void name(){
         System.out.println("XYZ");
        }

       public void roll(int n){
         System.out.println("12");
        }
     }

class Stu extends Example {
    public void name(){
      System.out.println("LKJ");
  }

    public void roll(int n){
      System.out.println("21");
    }

  public static void main(String[] args) {
  
    Stu obj = new Stu();
   obj.name();
   obj.roll(1);
   }
    }

 

Output :

 
  
  LKJ
  21
  

Example

class Example{
       public void name(){
         System.out.println("XYZ");
        }

       public void roll(int n){
         System.out.println("12");
        }
     }

class Stu extends Example {
    public void name(){
      System.out.println("LKJ");
  }

    public void roll(int n){
      System.out.println("21");
    }

  public static void main(String[] args) {
  
    Example obj = new Example();
   obj.name();
   obj.roll(1);
   }
    }

 

Output :

 
  
  XYZ
  12
  
Advertisment

Cannot create child reference in parent object it will give compile-time error

Example

class Parent{
       public void money(){
         System.out.println("1 lakh");
        }
     }

class Child extends Parent{
    public void name(){
      System.out.println("50,000 thousand");
  }

  public static void main(String[] args) {
  
   Child c= new Parent();
   c.money();
  
   }
    }

 

Output :

 
  
 incompatible types: Parent cannot be converted to Parent.Child
 Child c = new Parent();
 1 error
  

java Runtime Polymorphism with Multilevel Inheritance

class Animal{  
void elephant(){System.out.println("Forest animal");}  
}  
class Animal1 extends Animal{  
void dog{System.out.println("pet animal");}  
}  
class Animal2 extends Animal1{  
void lion(){System.out.println("forest king animal");}  
public static void main(String args[]){  
Animal a1,a2,a3;  
a1=new Animal();  
a2=new Animal1();  
a3=new Anaimal2();  
a1.elephant();  
a2.dog();  
a3.lion();  
}  
}  

 

Output :

 
  
  forest animal
  pet animal
  forest king animal
  
Advertisment
Advertisment
arrow_upward