Java Polymorphism

The word polymorphism is derived from greek words which means many forms.

Polymorphism is an important concept in object-oriented programming language. Its the ability to take more than one form.

Polymorphism in Java can be achieved in the following ways:

  • Method Overriding
  • Method Overloading

Java Method Overriding

If the same method is present in both the superclass and the subclass during inheritance in Java, then the method in the subclass overrides the method in the superclass. This is known as method overriding.

Example for method overriding

class Language {
  public void displayLanguage() {
    System.out.println("Common English Language");
  }
}

class JavaProgramming extends Language {
  public void displayLanguage() {
    System.out.println("Java Programming Language");
  }
}

class Main {
  public static void main(String[] args) {

    // create object for JavaProgramming class
    JavaProgramming javaObj = new JavaProgramming();
    javaObj.displayLanguage();

    // create object for Language class
    Language langObj = new Language();
    langObj.displayLanguage();
  }
}

Output

Java Programming Language
English Language

In the above program, we have Language class with a method displayLanguage(). Then we create a class JavaProgramming and inherit the Language class.

Now, we need a seperate definition for the displayLanguage() menthod in JavaProgramming class. So we override that method with new definition.

Next, we create an object for JavaProgramming class and call the displayLanguage() method to display its own implementation.
Similarly, we will create an object for the Language class and call the displayLanguage() method.

Method overloading

In java, if we create the methods with the same name, but with different number of parameters then we call it as method overloading.

Example for method overloading

  class Addition {
  int x,y;
  Addition(int a, int b){
      x = a;
      y = b;
  }
  int add() {
    return x + y;
  }
  int add(int a, int b) {
    return a + b;
  }
  int add(int a, int b, int c) {
    return a + b + c;
  }
}
class Main {
  public static void main(String[] args) {
    Addition addObj = new Addition(34,56);
    // Call add() function
    System.out.println("Sum of 34 and 56 is "+addObj.add());
    
    // Call add(int a, int b) function
    System.out.println("Sum of 55 and 57 is "+ addObj.add(55,67));
    
        // Call add(int a, int b, int c) function
    System.out.println("Sum of 78,65 and 74 is "+addObj.add(78,65,74));
  }
}

Output

Sum of 34 and 56 is 90
Sum of 55 and 57 is 122
Sum of 78,65 and 74 is 217

In the above program, we have a class named Addition with 3 methods with same name add.

The add() method will add the value of x and y and return the result value. The x and y values are initialized in the constructor of the class.

The add(int a, int b) method takes two integer parameters and return the sum of a and b.

The add(int a, int b, int c) method takes three integer parameters and returns the sum of a, b and c.

Now in the Main class, we create object for the Addition class and call the above 3 methods and print their results.


Most Read