Tuesday, April 14, 2020

What is Polymorphism in Java? Overriding or Overloading?

Polymorphism vs Overloading vs Overriding


Someone asked me What are the difference between Polymorphism and Overriding in Java and the similar difference between Polymorphism and Overloading. Well, they are not two different things, Polymorphism is an object oriented or OOPS concept like Abstraction, Encapsulation or Inheritance which facilitate the use of the interface and allows Java program to take advantage of dynamic binding in Java. Polymorphism is also a way through which a Type can behave differently than expected based upon which kind of Object it is pointing. Overloading and overriding are two forms of Polymorphism available in Java.

Polymorphism in Java, Oracle Java Tutorial and Materials, Oracle Java Certification, Oracle Java Prep

Both overloading and the overriding concept are applied on methods in Java. Since polymorphism literally means taking multiple forms, So even though you have the name of the method same in the case of overloading and overriding, an actual method called can be any of those multiple methods with the same name.

Polymorphism vs Overriding


Overriding is a form of polymorphism which is used in Java to dynamically bind method from the subclass in response to a method call from sub class object referenced by superclass type. Method overriding is bonded using dynamic binding in Java.

Suppose you have two methods size() in both base class and derived class and Base class variable is pointing to an object which happens to be subclass object at runtime then method from subclass will be called, i.e. overridden method will be called.

This allows to program for interface than implementation, a popular OOPS design principle because Polymorphism guarantees to invoke correct method based upon the object. Method overriding is key for much flexible design pattern in Java.

Polymorphism vs Overloading


Method overloading is another form of Polymorphism though some people argue against that. In the case of overloading, you also got multiple methods with the same name but different method signature but a call to correct method is resolved at compile time using static binding in Java. Overloading is a compile time activity oppose to Overriding which is runtime activity. Because of this reason overloading is faster than method overriding in Java. Though beware with an overloaded method which creates conflict e.g. methods with only one parameter e.g. int and long etc.

Polymorphism in Java, Oracle Java Tutorial and Materials, Oracle Java Certification, Oracle Java Prep

An example of Polymorphism in Java


Let's see a short example of Polymorphism in Java. In this example, Pet variable behaves polymorphic because it can be either Cat or Dog. this is also an example of method overriding because makeSound() method is overridden in subclass Dog and Cat.

import java.util.ArrayList;
import java.util.List;

abstract class Pet{
    public abstract void makeSound();
}

class Cat extends Pet{

    @Override
    public void makeSound() {
        System.out.println("Meow");
    } 
}

class Dog extends Pet{

    @Override
    public void makeSound() {
        System.out.println("Woof");
    }

}

Let's test How Polymorphism concept work in Java:

/**
 *
 * Java program to demonstrate What is Polymorphism
 * @author Javin Paul
 */
public class PolymorphismDemo{

    public static void main(String args[]) {
        //Now Pet will show How Polymorphism work in Java
        List<Pet> pets = new ArrayList<Pet>();
        pets.add(new Cat());
        pets.add(new Dog());
   
        //pet variable which is type of Pet behave different based
        //upon whether pet is Cat or Dog
        for(Pet pet : pets){
            pet.makeSound();
        }
 
    }
}

Output:
Meow
Woof

In Summary, you can not compare Polymorphism with method overloading or override. Polymorphism is the ability of a variable to behave differently based upon which kind of Object it is referring. They are Java programming language's way to implement polymorphism in language.

Related Posts

0 comments:

Post a Comment