method overloading VS method overriding
Method overloading and method overriding are two key concepts in Java that allow developers to define multiple methods with the same name but different functionalities. Here’s a detailed comparison of the two:

Table of Contents
Method Overloading
- Definition: Method overloading occurs when multiple methods in the same class have the same name but different parameters (different type, number, or both).
- Compile-Time Polymorphism: Overloading is resolved during compile time.
- Parameter Differences: The methods must differ in their parameter lists (either by number, type, or order of parameters).
- Return Type: The return type can be the same or different, but it doesn’t distinguish overloaded methods.
- Same Class: Overloaded methods must be in the same class or in a subclass.
- Inheritance Not Required: Overloading can occur without inheritance.
Example
class MathOperation {
// Overloaded methods
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
MathOperation op = new MathOperation();
System.out.println(op.add(2, 3)); // Calls add(int, int)
System.out.println(op.add(2.5, 3.5)); // Calls add(double, double)
System.out.println(op.add(1, 2, 3)); // Calls add(int, int, int)
}
}
Method Overriding
- Definition: Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.
- Runtime Polymorphism: Overriding is resolved at runtime.
- Same Parameters: The overriding method must have the same name, return type, and parameters as the method in the superclass.
- Return Type: The return type must be the same or a subtype (covariant return type) of the return type in the superclass.
- Inheritance Required: Overriding requires a superclass and a subclass (inheritance).
- @Override Annotation: The @Override annotation is commonly used to indicate that a method is overriding a superclass method.
Example
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myDog = new Dog();
myAnimal.sound(); // Calls Animal's sound()
myDog.sound(); // Calls Dog's overridden sound()
}
}
Key Differences:
- 1. Purpose:
- Overloading: To increase the readability of the program by allowing different ways to perform similar tasks.
- Overriding: To provide specific implementation for a method that is already defined in a superclass.
- 2. Binding:
- Overloading: Compile-time (static) binding.
- Overriding: Runtime (dynamic) binding.
- 3. Parameters:
- Overloading: Different parameter lists.
- Overriding: Same parameter list.
- 4. Inheritance:
- Overloading: Not dependent on inheritance.
- Overriding: Dependent on inheritance.
- 5. Return Type:
- Overloading: Can have different return types.
- Overriding: Must have the same or a covariant return type.
Understanding these differences helps in effectively utilizing these concepts to design robust and flexible Java applications.