Method Overloading & Method Overriding

Lalji Prajapati
4 min readJul 14, 2021
  1. Method Overloading:-

method overloading is the process of creating multiple methods inside the class with the same method name and with a different parameter list.

Method overloading can be achieved by using the instance method, static method, static final method, and instance final method.

During method overloading, the compiler will perform a method regulation based on a parameter list.

  1. Increasing the number of parameters in a parameter list.
  2. Changing the data type in a parameter list.
  3. Interchanging the parameter in a parameter list.
Method overloading
  1. Increasing the number of parameters in a parameter list.

for example……..

class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}

Output:

2. Changing the data type in a parameter list.

for example…………

class DisplayOverloading2
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(int c)
{
System.out.println(c );
}
}

class Sample2
{
public static void main(String args[])
{
DisplayOverloading2 obj = new DisplayOverloading2();
obj.disp('a');
obj.disp(5);
}
}

Output:

a
5

3. Interchanging the parameter in a parameter list.

for example………

class DisplayOverloading3
{
public void disp(char c, int num)
{
System.out.println("I’m the first definition of method disp");
}
public void disp(int num, char c)
{
System.out.println("I’m the second definition of method disp" );
}
}
class Sample3
{
public static void main(String args[])
{
DisplayOverloading3 obj = new DisplayOverloading3();
obj.disp('x', 51 );
obj.disp(52, 'y');
}
}

Output:

I’m the first definition of method disp
I’m the second definition of method disp

A. Method Overloading with Type Promotion if matching found

for example……

class OverloadingCalculation2{

void sum(int a,int b){System.out.println(“int arg method invoked”);

}

void sum(long a,long b){System.out.println(“long arg method invoked”);

}

public static void main(String args[]){

OverloadingCalculation2 obj=new OverloadingCalculation2();

obj.sum(20,20);//now int arg sum() method gets invoked

}

}

Output:

Output:int arg method invoked

B. Method Overloading with Type Promotion in case of ambiguity

for example……..

class OverloadingCalculation3{

void sum(int a,long b){System.out.println(“a method invoked”);}

void sum(long a,int b){System.out.println(“b method invoked”);}

public static void main(String args[]){

OverloadingCalculation3 obj=new OverloadingCalculation3();

obj.sum(20,20);//now ambiguity

}

}

Output:

Output:Compile Time Error

2. Method Overriding:-

Method overring is a process of creating a subclass method having return type method name and parameter list matching with the return type method name and parameter list of the superclass method.

The subclass method will always override the superclass method.

During method overriding the superclass method will be hidden and the subclass method will be exposed, this is called a method hiding.

The Method overriding is support with the instance method and not supported with the static method, instance final method, and static final method.

The instance final method can be inherited but can not be overridden.

Note:-

During non-overriding synchronize the programmer can access the superclass method inside the subclass object.

for example……….

class Human{
//Overridden method
public void eat()
{
System.out.println("Human is eating");
}
}
class Boy extends Human{
//Overriding method
public void eat(){
System.out.println("Boy is eating");
}
public static void main( String args[]) {
Boy obj = new Boy();
//This will call the child class version of eat()
obj.eat();
}
}

Output:

Boy is eating

2. A real example of the Java Method Overriding

method voerriding

//Java Program to demonstrate the real scenario of Java Method Overriding

//where three classes are overriding the method of a parent class.

//Creating a parent class.

class Bank{

int getRateOfInterest(){return 0;}

}

//Creating child classes.

class SBI extends Bank{

int getRateOfInterest(){return 8;}

}

class ICICI extends Bank{

int getRateOfInterest(){return 7;}

}

class AXIS extends Bank{

int getRateOfInterest(){return 9;}

}

//Test class to create objects and call the methods

class Test2{

public static void main(String args[]){

SBI s=new SBI();

ICICI i=new ICICI();

AXIS a=new AXIS();

System.out.println(“SBI Rate of Interest: “+s.getRateOfInterest());

System.out.println(“ICICI Rate of Interest: “+i.getRateOfInterest());

System.out.println(“AXIS Rate of Interest: “+a.getRateOfInterest());

}

}

Output:-

Output:
SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9

--

--