Difference between Overloading and Overriding in Java

Below are the difference between Method Overriding and Method Overloading in Java:

Point of Distinction Method Overloading Method Overriding
Definitions Overloading occurs when two or more methods in one class have the same method name but different parameters. Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class.
Use Method overloading increases the readability of the program. Method overriding provides the specific implementation of the method that is already provided by its super class.
Binding of method Static Dynamic
Compiler Time / Run Time The binding of overloaded method call to its definition has happens at compile-time. Binding of overridden method call to its definition happens at run time.
No. of Classes Method overloading is occurs within the same class. Method overriding occurs in two classes that have IS-A relationship.
Parameters In this case, parameter must be different. In this case, parameter must be same.
Static Method Static methods can be overloaded which means a class can have more than one static method of same name. Static methods cannot be overridden.
Performance Better than Overriding Less, reason is that the binding of overridden methods is being done at run time.
Private and final Method Can be overloaded. Can not.
Return Type Return type of method does not matter in case of method overloading, it can be same or different. In case of method overriding the overriding method can have more specific return type
Example See code below see code below

Method Overloading Example

package com.javademo;

class SumNumbers
{
    int add(int num1, int num2) 
    {
        return num1 + num2;
    }
    
    double add(double num1, double num2) 
    {
        return num1 + num2;
    }
    
    int add(int num1, int num2, int num3) 
    {
        return num1 + num2 + num3;
    }
 
    public static void main(String args[])
    {
    	SumNumbers S1 = new SumNumbers();
    	System.out.println("Sum of Numbers: " +S1.add(10, 20));
    	System.out.println("Sum of Numbers: " +S1.add(10.5, 20.5));
    	System.out.println("Sum of Numbers: " +S1.add(10, 20, 30));
    
    }
}

Output

Sum of Numbers: 30

Sum of Numbers: 31.0

Sum of Numbers: 60

Method Overriding Example

package com.javademo;

class Vehicle
{
    public int speedLimit() 
    {
        return 30;
    }
}

class TwoWheeler extends Vehicle
{
	public int speedLimit()
	{
		return 40;
	}
}

class FourWheeler extends Vehicle
{
    public int speedLimit()
    {
        return 60;
    }
} 

class CheckSpeed
{
    public static void main(String args[])
    {
    	FourWheeler F1 = new FourWheeler();
    	int fSpeed = F1.speedLimit();
    	System.out.println("Speed Limit is: "+fSpeed);
    	
    	TwoWheeler T1 = new TwoWheeler();
    	int tSpeed = T1.speedLimit();
    	System.out.println("Speed Limit is: "+tSpeed);
    	
    }
}

Output

Speed Limit is: 60

Speed Limit is: 40

Comments