Method Overloading And Method Overriding in JAVA

Overloading and Overriding in Java

Bijen Adhikari
6 min readMay 8, 2023

Overloading and overriding methods are two fundamental topics in Java object-oriented programming. Both notions are connected to the operation of Java methods, yet they serve different functions.

Method Overloading:

Method Overloading is a technique that allows you to declare numerous methods with the same name but different parameters in the same class. In other words, you can have several methods with the same name in a class as long as they take different parameters.

IF

  • A class has multiple methods having same name
  • but have different parameters. This is called method overloading.
  • In method overloading we do not need parent-child relationship.

For example, we have a class name Calculator, and we want to define a method named subtraction() that can subtract two integers or doubles. We can do this using method overloading as shown below:

package polymorphism1;

class Calculator{
public int subtraction(int n1,int n2,int n3) {
return n1-n2-n3;
}
public int subtraction(int a,int b) {
return a-b;
}

}

public class polymorphism {

public static void main(String[] args) {

Calculator obj=new Calculator();
int k1= obj.subtraction(55,10,10);
System.out.println(k1);

}

}

--

--

Bijen Adhikari

Hi, I am Bijen from Nepal I write about Electronics, Electrical, Programming, Fitness. Also, I am a certified SSI Master Trainer.