Java Polymorphism

What is java polymorphism |Method Overloading | Method Overriding

Polymorphism means many forms, and it occurs when we have many classes that are related to each other by inheritance.

Inheritance let us inherit attributes and methods from another class.

Polymorphism uses those methods to perform different tasks. this allows us to perform a single action in different ways.


If a class has multiple methods having same name but different in parameters , it is known as method overloading.

If we have to perform only one operation , having same name of the method increases the readability of the program.

Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a (int,int) for two parameters.

and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behaviour of the method because its name differs.

Advantage of method overloading :-

Method overloading increases the readability of the program.

Different ways to overload the method :-

There are two ways to overload the method in java.

1.By Changing number of arguments.

2.By Changing the Data type.

1.By Changing number of arguments:-

public class Adder {
    static  int add(int a, int b) {
        return  a+ b;
    }
    static  int add(int a, int b,int c) {
        return  a+ b +c;
    }


}

class  Testoverloading1{

    public static  void main(String[] args){
        System.out.println(Adder.add(11,11));
        System.out.println(Adder.add(11,11,11));

    }
}

2.By Changing the Data type:-

public class Adder {
    static  int add(int a, int b) {
        return  a+ b;
    }
  

    static  double add(double a, double b) {
        return  a+ b;
    }

}

class  Testoverloading1{

    public static  void main(String[] args){
        System.out.println(Adder.add(11,11));
        System.out.println(Adder.add(12.5,2.5));
      

    }
}



Method Overriding in java :-

If Subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.

In other words, if a Subclass provides the specific implementation of the method that has been declared by one of its parents class, it is known as method overriding.

Usages of java method Overriding :-

Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.

Method overriding is used for runtime polymorphism.

Rules for Java method Overriding :-

1.The method must have same name as in the parent class.

2.The method must have the same parameter as in the parent class.

3.There must be an IS-A Relationship (inheritance).

class Car{  
  //defining a method  
  void run(){System.out.println("Car is running");}  
}  
//Creating a child class  
class Car2 extends Vehicle{  
  //defining the same method as in the parent class  
  void run(){System.out.println("Car is running safely");}  
  
  public static void main(String args[]){  
  Car2 obj = new Car();//creating object  
  obj.run();//calling method  
  }  
}  

Output :- Car is running safely