Method Overloading and Method Overriding in Java

Method Overloading:

In Java, it is possible to create methods that have the same name, but different parameter lists and different definitions that are called Method Overloading. It is used when objects are required to perform similar tasks but using different input parameters.

Example :

class House
{
 int length, width;
House(int x, int y)     // creating constructor 1
{
 length=x;
 width=y;
}
House(int x)           // creating constructor 2
{
length=width=x;
}
int area()
{
return(length*width);
}
public static void main(String arg[])
{
 House h1=new House(); 
 System.out.println(h1.House(20,16));
 House h2=new House();
 System.out.println(h2.House(20));
}
}

Output :
320
20

Method Overriding:

When a method in the subclass that has the same name, same arguments and same return type as a method in the superclass. Then the method defined in the subclass and it is invoked and executed instead of the one superclass. That is called Method Overriding.

class super
{
 int x;
super(int x)
{
 this.x=x;
}
void display()
{
System.out.println("Super x :"+x);
}
}
class add extends super 
{
 int y;
add(int x, int y)
{
 super(x);
this.y=y;
}
void display()
{
 System.out.println("Super x :"+x);
 System.out.println("add y :"+y);
}
}
class test
{
 public static void main(String args[])
{
add a1=new add(50, 100);
a1.display();
}
}

Output:
super x : 50
add y : 100