Method overloading vs Method Overriding Java

Method overloading and method overriding are two concepts supported by a OOPs language.

Method overloading

Definition: Using the same method number of times in the same class but with different parameters is known as method overloading.

Advantages: The same method call gives different outputs when called different times. For example, getArea() method may give sometimes circle area and sometimes square area.

Following is an example on Method Overloading.

public class Figures
{
  public void getArea(int side)           // I,  getArea() gives circle area
  {
    System.out.println("Circle area: " + Math.PI*side*side);
  }
  public void getArea(double side)	  // II, getArea() gives square area
  {
    System.out.println("Square area: " + side*side);
  }
  public static void main(String args[])
  {
    Figures f1 = new Figures();
    f1.getArea(10);                       // calls I
    f1.getArea(10.5);                     // calls II
  }
}

ima

The getArea() method is overloaded two times with different parameters of int and double.

f1.getArea(10);
f1.getArea(10.5);

The getArea() is called two times passing int and double values. The output gives circle area and square area. This is called method overloading. If the Programmer would like to use the same method name for different functionalities (outputs), he prefers method overloading.

Method Overriding

Method overloading occurs within the class but method overriding occurs between two classes involved in inheritance.

Definition: If the super class and subclass have the same method, we say, the super class method is overridden by subclass. In overriding, super class and subclass will have the same method with same parameters and same return type.

Following is an example on method overriding.

class Surgeon
{
  public void woundTreatment()
  {
    System.out.println("Stich the wound");
  }
}

public class AsstSurgeon extends Surgeon
{
  public void woundTreatment()
  {
    System.out.println("Leave the wound to air as it is dog bite");
  }
  public static void main(String args[])
  {
    AsstSurgeon as1 = new AsstSurgeon();
    as1.woundTreatment();
  }
}

ima1

Both the super class and subclass have the same method woundTreatment(). We say, super class woundTreatment() method is overridden by subclass.

as1.woundTreatment();

In case of method overriding, the subclass object will call its own method. Observe the output screen. As long as subclass overridden method exists, the subclass object cannot call super class woundTreatment() method.

When to use: If the subclass would like to change the functionality of super class method as in the above example, subclass overrides the method with its own functionality (output). In the above code, Surgeon wanted to stitch the wound but the AsstSurgeon wanted to leave the wound to air.

Following table gives the differences between Method overloading vs Method Overriding.
Method Overloading Method Overriding
Occurs within the same class Occurs between two classes
Inheritance is not involved Inheritance is involved
One overloaded method does not hide the other method Subclass overridden method hides super class method from access by subclass object
Parameters should be different Parameters should be the same
Return type may or may not be the same Return type should be the same
Leads to static binding and static polymorphism Leads to dynamic binding and dynamic polymorphism

Now I am sure you are very clear of Method overloading vs Method Overriding.

View All for Java Differences on 90 Topics

5 thoughts on “Method overloading vs Method Overriding Java”

  1. I need the reason why we call method overriding as dynamic binding??
    Please provide me with a clarity information along with an example.

Leave a Comment

Your email address will not be published.