Overloading and Overriding Java

Overloading and Overriding, are two terms very confusing for a for a Beginner learning Java. Read this, and say you are still confused. Both are supported by OOPS languages like Java and C++ but not by C-lang.

To explain, I take it granted you know methods and inheritance in Java.

At an outset, to know the difference in simple, overloading is a feature which occurs within the same class and overriding in between two classes linked with extends keyword. If you understand this much, it is enough to go further into the subject of Overloading and Overriding. To start with let us see first overloading directly with an example and later go for explanation what exactly overloading means.

public class Area
{
  public void calculate()                      // I
  {
    System.out.println("I do not calculate as I do not have any data");
  }  
  public void calculate(int x)                 // II
  {
    System.out.println("Circle Area: " + Math.PI*x*x);
  }
  public void calculate(int x, int y)          //III
  {
    System.out.println("Rectangle Area: " + x*y);
  }
  public static void main(String args[])       // now main() to call the methods
  {
    Area a1 = new Area();
    a1.calculate();                            // calls I    
    a1.calculate(10);                          // calls II
    a1.calculate(10, 20);                      // calls III
  }
}


ssOutput screenshot of Area.java on Overloading and Overriding

In the above class Area, calculate() method is defined three times, of course with different parameters. The first one with no parameters, second one with single int parameter and the third one with two int parameters. By observation, let us define what overloading is. Defining the same method in the same class number of times is known as method overloading. But remember they must have different parameters, else the compiler does not know and gets confused which overloaded method is to be called when you need.

In the main(), when you call a1.calculate(10), compiler knows very well that you would like to call calculate() method with int parameter as 10 is int. Similarly, when you call a1.calculate(10, 20), it is well known to compiler that you need calculate() with two int parameters as 10 and 20 are int. Like this depending on the parameters you pass, compiler can easily differentiate which overloaded method is to be called.

Then, you may ask why this concept is introduced in OOPS languages. With single method remembering, you can calculate circle area and also rectangle area. Infact, it leads to static binding and static polymorphism. This you do not bother much and see later.

Now let us go for method overriding of Overloading and Overriding. Observe the code.

class HeadOffice
{
  public void productionTarget()         // I
  {
    System.out.println("Produce 10000 pieces");
  }
}
public class BranchOffice extends HeadOffice
{
  public void productionTarget()         // II
  {
    System.out.println("Produce 5000 pieces only");
  }
  public static void main(String args[])
  {
    BranchOffice b1 = new BranchOffice();
    b1.productionTarget();               // calls II
  }
}


ssOutput screenshot of BranchOffice.java on Overloading and Overriding

In the above code, productionTarget() method is defined in both super class HeadOffice and subclass BranchOffice. Having the same method in the super class and subclass is known as method overriding. We say, super class method productionTarget() is overridden by subclass. In case of method overriding, subclass object b1 calls its own productionTarget() method and not of super class.

Why method overriding is introduced? Observe, the HeadOffice imposed a target to make 10000 pieces. But BranchOffice in a different location, due to public strikes, unable to to go as per head office plan, overrides 10000 with 5000 pieces. This is the advantage. That is subclass can use the same super class method without writing altogether a new method.

To know further more on these concepts go though the links.

1. Using Methods and Method Overloading
2. Method Overriding
3. Table of differences overloading vs Overriding
4. Can we Overload Main?

Leave a Comment

Your email address will not be published.