Method Overloading and Overriding in java

Let’s learn what is method overloading and overriding in java?

Method Overloading and Overriding in java

Method Overloading in java

Method overloading means two or more discrete methods in a class with the same name but with different parameters.

Example:

class Addition
{
   static int add(int a, int b)
   {
      return a + b;
   }  
   static int add(int a, int b, int c)
   {
      return a + b + c;
   }  
}  
class OverloadingMethodExample
{  
   public static void main(String[] args)
   {
      System.out.println(Addition.add(5, 5));  
      System.out.println(Addition.add(5, 5, 5));  
   }
}

Output:

10
15


What is the purpose of method overloading?

Method overloading is very user-friendly, it reduces duplicated code and programmer need not have to remember multiple method names.

In method overloading return type may or may not be unlike and that helps programmers to reuse the same method name.

Often method overloading is referred to compile time polymorphism. In other words the compiler decides which method is going to be called based on the method name, return type and number of arguments.


Rules for method overloading in java

Below are the rules:

  1. Methods must have same method name.
  2. Methods must have different parameters.

If methods follow the rules for method overloading then methods may or may not,

  1. have different return data types.
  2. have different access modifiers.
  3. throw different checked or unchecked exceptions.

Method Overriding in java – means creating a method in a sub class or child class which already exists in the super class or parent class with same name and same arguments.

Child class which extends parent class gets all the methods defined in the parent class. 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.

To override a method it’s recommended to put @Override, an annotation, immediately above method definition. This is an annotation that compiler reads and will show error if we do not follow overriding rules. 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 of method overriding

A method is considered overridden if,

  1. Method must have same name and same arguments.
  2. Method return type can be subclass of return type in parent class.
  3. 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.

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.

Difference between method overloading and overriding in java

Method overloadingMethod overriding
Method overloading usually in single class. May also used in child classMethod overriding always in two classes that have IS-A relationship.
It helps functionality in reusing a method name with different parameters.Use to override a behaviour which the class has inherited from parent class.
Method overloading must have different parameters.Method overriding must have same parameters and same name.
Method overloading may have different return types.Method overriding must have same return type or covariant return type (child class).
Method overloading may have different access modifiersMethod overriding must not have lower access modifier but may have higher access modifier.
Method overloading may throw different exceptions.Method overriding must not throw new or broader checked exceptions