Method Overloading and Method Overriding

Method Overloading
- Method overloading occurs when you have multiple methods with the same name but different parameter lists.
- This can be done in the same class.
Example:
class Calculator {
// Method to add two integers
int add(int a, int b) {
return a + b;
}
// Overloaded method to add three integers
int add(int a, int b, int c) {
return a + b + c;
}
// Overloaded method to add two double values
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(10, 20)); // Output: 30 (int version)
System.out.println(calc.add(10, 20, 30)); // Output: 60 (3 int version)
System.out.println(calc.add(10.5, 20.5)); // Output: 31.0 (double version)
}
}
- Explanation: In this example, the
add
method is overloaded with different parameter types and counts. The compiler will choose the appropriate method at compile time based on the arguments passed.
Method Overriding
- Method overriding occurs when a subclass provides its own implementation of a method that is already defined in the superclass.
Example:
class Animal {
// Method in the parent class
void sound() {
System.out.println(“Animal sound”);
}
}
class Dog extends Animal {
// Overriding the method in the child class
@Override
void sound() {
System.out.println(“Bark”);
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Creating an object of the parent class
myAnimal.sound(); // Output: Animal sound
Animal myDog = new Dog(); // Creating an object of the subclass
myDog.sound(); // Output: Bark
}
}
- Explanation: In this example, the
sound
method is overridden in theDog
class to provide a more specific implementation. At runtime, Java will use theDog
class’ssound
method (since the object is of typeDog
), even though the reference is of typeAnimal
. This is an example of runtime polymorphism.
Method Overloading vs. Method Overriding
Feature | Method Overloading | Method Overriding |
---|---|---|
Definition | Method overloading occurs when multiple methods have the same name but differ in parameters (either in number, type, or both) within the same class. | Method overriding occurs when a subclass provides a specific implementation for a method already defined in its parent class with the same method signature. |
Purpose | It allows multiple methods to perform similar tasks with different types or numbers of parameters. | It allows a subclass to provide a specific behavior for a method that is already defined in its superclass. |
Method Signature | In overloading, the method name is the same, but the parameters (number, type, or order) are different. | In overriding, the method signature must be the same, i.e., the same name and same parameters (type, number, and order). |
Return Type | The return type can be different or the same for overloaded methods, but it doesn’t differentiate between overloaded methods. | The return type must be the same or a subtype (covariant return type) as in the parent class. |
Time of Binding | Compile-time polymorphism (Static Binding). The method to be called is determined at compile time. | Runtime polymorphism (Dynamic Binding). The method to be called is determined at runtime, depending on the object type. |
Inheritance | No inheritance involved. Overloading can occur within the same class. | Involves inheritance. The subclass overrides a method inherited from its parent class. |
Access Modifiers | Overloaded methods can have different access modifiers (public, private, protected). | The access modifier in the subclass method should be the same or more permissive than the parent method’s access modifier. |
Method Signature Impact | Overloading is determined based on the method signature (name + parameters). | Overriding requires an exact match in method signature (name + parameters). |
Example | java class Animal { void sound() { System.out.println("Animal sound"); } void sound(String type) { System.out.println(type + " sound"); } } | java class Animal { void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { @Override void sound() { System.out.println("Bark"); } } |