Method Overloading and Overriding in java

Let’s learn method overloading and overriding in java.

Method Overloading and Overriding in java

Method overloading means two or more methods in a class having the same name but different argument types.

Example:

class Addition
{
   void display(int a, int b)
   {
      System.out.println("2");
   }  
   void display(int a, int b, int c)
   {
      System.out.println("3");
   }
   public static void main(String[] args)
   {
      Addition add = new Addition();
      add.display(10, 20);
   }
}

Output:

2


Advantage of method overloading

Method overloading gives more flexibility to the programmer we can use same method name eventhough argument types are different.


Rules for method overloading in java

Below are the rules:

  1. Multiple methods must have same name.
  2. Methods should be in same class.
  3. Methods must have different argument types.
  4. Method signatures must be different.
  5. Return type can be same or different return type.

Method Overriding in java – means overriding parent class method in child class with same name and same arguments.

Parent class method which is overridden is called overridden method and child class method which is overriding is called overriding method.

Method overriding is also known as Runtime polymorphism (dynamic method dispatch) because method that is going to be called is decided at runtime by java virtual machine.

Some more important points on method overriding in java:

  1. Methods which are “final” cannot be overridden.
  2. Private methods and constructors cannot be overridden.
  3. Methods can be overridden only in child classes.
  4. Child class can use super method to call parent class version of an overridden method.

Let’s see an example,

// parent class
class Car
{
   // defining method
   public void accelerate()
   {
      System.out.println("Car accelerating");
   }
}
// child class
class Mahindra extends Car
{
   // defining same method in child class
   @Override
   public void accelerate()
   {
      System.out.println("Mahindra accelerating");
   }
}
public class TestMethodOverriding
{
   public static void main(String[] args)
   {
      Mahindra obj = new Mahindra();
      obj.accelerate();
   }
}

Output:

Mahindra accelerating

NOTE: We can override only instance methods not static methods.


Rules for method overriding

A method is considered overridden if,

  1. Two or more methods should have same name.
  2. Methods should be in different classes.
  3. Method signatures must be same.
  4. Methods should have same argument types including order.
  5. Method return type must be same till Java 1.4 version. After Java 1.5 version use co-varient return type.
  6. Method cannot have lower class access modifier. For example: if parent class method is protected then using private in child class is not allowed but using public in child class is allowed.

Difference between method overloading and overriding in java

Method overloadingMethod overriding
private, static and final methods can be overloaded.private, static and final methods cannot be overridden.
Any access modifiers can be overloaded there is no restriction.While method overriding we can increase scope of access modifier. But cannot reduce scope of access modifier.
In method overloading there are no restrictions on throws clause.In method overriding if child class throws any exception parent class should throw same checked exception or its parent exception.
In method overloading method argument types must be different.In method overriding method argument type must be same.
Methods signatures must be different.methods signatures must be same.
Method return type have no restrictions.Method return type must be same till java 1.4 version. From java 1.5 version co-variant return type is allowed.