Polymorphism in java

It is one of major pillar of object-oriented programming. The name Polymorphism is combination of 2 word i.e. Poly means Many and Morphs means Forms. Hence the meaning of Polymorphism is many forms.

There are 2 types of polymorphism in java.

  1. Compile time Polymorphism: Achieved by method overloading.
  2. Runtime Polymorphism: Achieved by method overriding.
Method Overloading

In case of method overloading, method must have same name but can have different signature i.e. parameter list, parameter order, return type etc. Method overloading occurs in single class.

Below is example of method overloading.

In Math class given below we have 4 add methods with different signatures but with same method name as add.

Whenever add method is called from main, it is decided at compile time that add method with which signature is going to invoked at runtime.

Method Overriding

In case of method overriding, method must have same name and same signature. i.e. parameter list, parameter order, return type (or its subtype) etc.Method overriding occurs in multiple subclasses.

Below are the rules for overriding in java.

  1. Only inherited methods can be overridden.
  2. Final and static methods cannot be overridden.
  3. Overriding method must have same argument list.
  4. Overriding method must have same return type of subclass of return type.
  5. Overriding method must not have more restrictive access modifiers
    Default access –overridden method can have access modifier as Default, public, protected
    Protected access –overridden method can have access modifier as  public, protected
    Public access –overridden method can have access modifier as  public
  1. The overriding method must not throw new or broader checked exception.In other words overriding method may throw fewer or narrower checked exception or any unchecked exception.
  1. Use super keyword to invoke overridden method from sub-class.
  2. Constructor cannot be overridden.
  3. Abstract methods must be overridden by first concrete subclass.
  4. A static method in subclass may hide another static one in super class.
  5. The Synchronized modifier has no effect on the rule of overriding.
  6. The Strictfp modifier has no effect on the rule of overriding.

Below is example of method overriding.

Here getInterestRate() method of BankAccount class is overridden by its subclasses i.e. SavingsAccount and CurrentAccount with their own interest rates.

In main method of TestAccount class, account object reference will return rate of interest of the class object, which will get assigned to it. In first case it has received the CurrentAccount class object because of which it has called getInterestRate method of CurrentAccount class and returned result 4.2 as interest rate. In second case it has received the SavingsAccount class object because of which it has called getInterestRate method of SavingsAccount class and returned result 6.2 as interest rate.

Happy Learning!!

Leave a Reply

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