Home >>Java Tutorial >Polymorphism in Java

Polymorphism in Java

Polymorphism in Java

Polymorphism in Java is basically the ability of an object to take on many forms or in simple words in can be understood as one entity having many forms. The most widely usage of the polymorphism is in OOP as when a parent class reference is used in order to refer to a child class object. In the Java language, any Java object that is able to pass more than one IS-A test is generally considered to be polymorphic. In the Java language, since the process comprises of the fact that any object will have to pass the IS-A test for their own type and for the class Object hence, that makes all the Java objects polymorphic.

It is a known fact that and it is considered to be important that the only possible way from which programmers can access an object is only through a reference variable. In Java, a reference variable can only be of one type and it is to be noted that the type of the reference variable cannot be changed, once it has been declared. The reference variable in program can be easily reassigned to other objects provided that it is not declared finally. The type of the reference variable is very important in the Java as this is the one that would determine the methods whether it can invoke on the object or not. A reference variable in the Java language can refer to any of the object of its declared type or any subtype of the object of its declared type. In the Java language, a reference variable can generally be declared as a class or the interface type.

Here is an example of the polymorphism that will depict the exact definition of it with explaining the physical aspect of it:

public interface Vegetarian{}
public class Animal{}
public class Rabbit extends Animal implements Vegetarian{}

Now, in this above mentioned example, the Rabbit class is generally considered to be polymorphic as of the fact that this possesses multiple inheritances. Here are the true value for the above mentioned example:

  • A Rabbit IS-A Animal
  • A Rabbit IS-A Vegetarian
  • A Rabbit IS-A Rabbit
  • A Rabbit IS-A Object

Types of Polymorphism in Java language

There are generally two type of polymorphism in the Java language that are depicted below:

  • Method Overloading
  • Method Overriding

1. Method Overloading

Method Overloading is a condition in which a class consists of multiple methods that are having the exact same name but have different parameters. This is somehow beneficial in the case where the programmer have to perform only one operation that have the exact same name of the methods then it will automatically increases the readability of the program.

Example of Method Overloading

Here is an example of the method overloading in the Java language where two methods have been created, and the first operation consists of add() method that performs addition of two numbers and then the second add method performs addition of all the three numbers:

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));

}
    
} 
Output:
22
33

2. Method Overriding

Method Overriding is a condition in the Java language in which a subclass that is also known as the (child class) has the same method that has been declared in the parent class. In simple words, it can be understood as a case where a subclass provides the specific implementation of the method that has already been declared by one of its parent class. Please note that overriding generally means to override the functionality of an existing method in the terms of object-oriented.

Example of Method Overriding

Here is an example of the method overriding in the Java language. In the following depicted example, the run method has been defined in the subclass as this is defined in the parent class but it has some of the specific implementation. This example is the perfect example of the method overriding as the name and parameter of the method are exactly the same and there is IS-A relationship that exists between the classes:

//Java Program to illustrate the use of Java Method Overriding  
//Creating a parent class.  
class Vehicle
{
  
    //defining a method  
  void run ()
  {
    System.out.println ("Vehicle is running");
} 
} 
//Creating a child class  
  class Bike2 extends Vehicle
{
  
    //defining the same method as in the parent class  
  void run ()
  {
    System.out.println ("Bike is running safely");
  } 
 
public static void main (String args[])
  {
    
Bike2 obj = new Bike2 ();	//creating object  
    obj.run ();			//calling method  
} 
} 
Output:
Bike is running safely