Polymorphism in Java – Method Overloading | Method Overriding

Polymorphism is derived from 2 greek words: poly and morphs. The word “poly” means many and “morphs” means forms. So Polymorphism means the ability to take many forms. is a concept by which we can perform a single task in different ways. It is one of the most striking features of Object Oriented Programming in Java.

In terms of Java Programming Polymorphism is the capability of a method to do different things based on the object that it is acting upon. In other words, polymorphism allows you define one interface and have multiple implementations.

There are two types of polymorphism in java: compile time polymorphism(static or early binding) and runtime polymorphism(dynamic or late binding).

types of polymorphism in java

  1. Compile Time Polymorphism – Method Overloading (We have discussed this in detail in this article)
  2. Run Time Polymorophism – Method Overriding
Run Time Polymorphism

Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time. In this process, an overridden method is called through the reference variable of a superclass. Thus this happens only when Inheritance is implemented. The method to be called is based on the object being referred to by the reference variable.

  • When an overridden method is called through a superclass reference, Java determines which version(superclass/subclasses) of that method is to be executed based upon the type of the object being referred to at the time the call occurs. Thus, this determination is made at run time.
  • At run-time, it depends on the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed
  • A superclass reference variable can refer to a subclass object. This is also known as upcasting. Java uses this fact to resolve calls to overridden methods at run time.

dynamic polymorphism - upcasting

Lets see a program example to understand the working of run-time polymorphism –

Run Online

// A Simple Java program to demonstrate 
// method overriding in java
// Base Class
class Parent
{
    void show() { System.out.println("Parent's show()"); }
}
// Inherited class
class Child extends Parent
{
    // This method overrides show() of Parent
    @Override
    void show() { System.out.println("Child's show()"); }
}
 
// Driver class
class Main
{
    public static void main(String[] args)
    {
        // If a Parent type reference refers
        // to a Parent object, then Parent's
        // show is called
        Parent obj1 = new Parent();
        obj1.show();
 
        // If a Parent type reference refers
        // to a Child object Child's show()
        // is called. This is called RUN TIME
        // POLYMORPHISM.
        Parent obj2 = new Child();
        obj2.show();
    }
}
Output
Parent’s show()
Child’s show()

In the above example, you can see that depending on the type of Object (Parent or Child) the respective method show() is being called. Also using the parent object, we have created a reference to the child class (Parent obj2 = new Child()). Thus now the parent object is referring to the child class and hence we are able to access the child class’s show() method.

Some Rules to follow in Method Overriding –
  • Overriding and Access-Modifiers : The access modifier for an overriding method can allow more, but not less, access than the overridden method. For example, a protected instance method in the super-class can be made public, but not private, in the subclass.
  • Final methods can not be overridden.
  • Static methods can not be overridden(Method Overriding vs Method Hiding).
  • Private methods can not be overridden. 
  • The overriding method must have same return type (or subtype)
  • Invoking overridden method from sub-class :  We can call parent class method in overriding method using super keyword.
  • Overriding and constructor : We can not override constructor as parent and child class can never have constructor with same name(Constructor name must always be same as Class name).
  • Overriding and abstract method : Abstract methods in an interface or abstract class are meant to be overridden in derived concrete classes otherwise compile-time error will be thrown.

 

Leave a Reply

Your email address will not be published. Required fields are marked *