What is the difference between overloading and overriding?

Overloading:

Two methods are said to be overloaded if and only if both the methods have same name but different argument types.
Example:
public class MethodOverloading {

    public void test(int a)//overloaded
    {
        System.out.println("int argument method");
    }
   
    public void test(long a)//overloded    {
        System.out.println("long argument method");
    }
}

Overriding: 

 Overridding concept comes with inheritance. When a child class extends its parent class, all the methods of parent class are available to child class. If child class want to use any method available from parent class but with some modifications, then the child class has the flexibility to redefine the method based on its requirements.
The parent class method which is overridden is called overridden method and the child class method which overrides is called overriding method.
Example:
public class TestParentOverriding {
    public static void method1(int args)
    {
        System.out.println("method1 of main class");
    }
}
class TestChildOverriding extends TestParentOverriding
{
  public static void main(String[] args)
    {
        method1(5);
    } 
    public static void method1(int args)
    {
        System.out.println("method1 of child class");
    }
}
Output:
 method1 of child class

 Differences between overloading and overriding: 

The differences between method overloading and method overriding is defined in the below table based on the property.
Differences between overloading and overriding


No comments:

Post a Comment