Java method overloading & method overriding with easy explanation

In this tutorial,we will learn about the  java method overloading and method overriding step by step.

Method Overloading

When the class contains multiple methods with the same name but different parameter then it is known as method overloading.

It increases the code readability and save time.

How can achieve method overloading

1.By changing the number of arguments

2.By changing the data types.

1.By changing the number of arguments

int add(int a,int b)

int add(int a,int b,int c)

This example shows how we can achieve method overloading by changing the number of arguments. 

Example

class Sum
{
Public int add(int a,int b)
{
return (a+b);
}
Public int add(int a,int b,int c)
{
return (a+b+c);
}

Public static void main(String[ ] args)
{
Sum s1=new Sum( );
System.out.println(s1.add(1,2));
System.out.println(s1.add(1,2,3));
}
}

Output

3

6

2.By changing the data types

int sum(int x,int y)

float sum(float x,float y)

This example shows how we can achieve the method overloading by changing the data types.

Example:

class  Sum
{
Public int add(int x,int y)
{
return (x+y);
}
Public float add(float x,float y)
{
return (x+y);
}
Public static void main(String[ ] args)
{
Sum s1=new Sum( );//creating the object of Sum class to call the method
System.out.println(s1.add(1,2));
System.out.println(s1.add(10.5,10.4));
}
}

Output:

3

20.9

Note:Method overloading is the way to achieve the compile-time polymorphism.

Method Overriding

When the subclass or child class method contains same name,same parameter or signature and the same return of its parent class method.

This example shows that how can achieve method overriding.

Example:

Public class A
{
Public void sum( )
{
System.out.println(“This is parent class”);
}
}

Public class B extends A
{
Public void sum(  )
{
System.out.println(“This is child class”);
}
Public static void main(String[ ] args)
{
B obj=new B( );//creating the object of B class
A obj1=new B( ); //creating the object of A class which refers the B class object.
obj.sum( );
obj1.sum( );
}
}

Output

This is child class

This is parent class

Note:Method overriding is used to achieve the run time polymorphism in java.

Difference between method overloading and method overriding

Method OverloadingMethod Overriding

1.When the class contains multiple methods with the same name and parameter.
1.It provides the specific implementation of the child class methods that already present in the parent class.

2.It is used to achieve compile time polymorphism.


2.It is used to achieve runtime polymorphism.

3.It is performed within the class


3.It is performed by two classes parents & child class ie IS-A relationship.


4.Parameter must be different  in the case of method overloading

4.Parameter must be the same in the case of method overriding.

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

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