Java

Difference Between Method Overloading & Overriding

We will talk about the distinction between overloading and overriding in Java in this lesson. The following posts should be read if you are unfamiliar with these terms:

  1. Method overloading in java
  2. Method overriding in java

Overloading vs Overriding in Java

  1. Overloading occurs at compile time, but overriding occurs at run time. Overloaded method calls are bound to their definitions at compile time, but overridden method calls are bound to their definitions at run time.
  2. Overloaded static methods allow a class to have more than one static method with the same name. Static methods cannot be modified; even if the same static method is declared in a child class, it has no bearing on the parent class’s version of the method.
  3. The primary distinction is that whereas overriding calls for base and child classes, overloading is handled inside the same class. Giving the inherited method of the parent class a particular implementation is the whole point of overriding.
  4. Dynamic binding is used for methods that are overridden or overridden, whereas static binding is used for methods that are overloaded.
  5. Performance: Overloading outperforms overriding in terms of performance. The binding of overridden methods is carried out during runtime, which is the cause.
  6. Although they may be overloaded, private and final methods cannot be overridden. This implies that a class may have several identical private/final methods, but a child class may not override a base class’s private/final methods.
  7. In the case of method overloading, the return type might either be the same or different. However, if a method is overridden, the replacement method may have a more precise return type (refer this).
  8. While method overloading, the argument list should be distinct. The argument list for method overriding should match.

Overloading example

//A class for adding upto 5 numbers
class Sum
{
    int add(int n1, int n2) 
    {
        return n1+n2;
    }
    int add(int n1, int n2, int n3) 
    {
        return n1+n2+n3;
    }
    int add(int n1, int n2, int n3, int n4) 
    {
        return n1+n2+n3+n4;
    }
    int add(int n1, int n2, int n3, int n4, int n5) 
    {
        return n1+n2+n3+n4+n5;
    }
    public static void main(String args[])
    {
    	Sum obj = new Sum();
    	System.out.println("Sum of two numbers: "+obj.add(20, 21));
    	System.out.println("Sum of three numbers: "+obj.add(20, 21, 22));
    	System.out.println("Sum of four numbers: "+obj.add(20, 21, 22, 23));
    	System.out.println("Sum of five numbers: "+obj.add(20, 21, 22, 23, 24));
    }
}

Output:

Sum of two numbers: 41
Sum of three numbers: 63
Sum of four numbers: 86
Sum of five numbers: 110

We have four variations of the same procedure add here. The function add() is being overloaded in this case.

Overriding example

package beginnersbook.com;
class CarClass
{
    public int speedLimit() 
    {
        return 100;
    }
}
class Ford extends CarClass
{
    public int speedLimit()
    {
        return 150;
    }
    public static void main(String args[])
    {
    	CarClass obj = new Ford();
    	int num= obj.speedLimit();
    	System.out.println("Speed Limit is: "+num);
    }
}

Output:

Speed Limit is: 150

Related Articles

Leave a Reply

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

Back to top button