Method Overriding & Overloading
Definition
Method Overloading is the ability to define multiple methods with the same name in the same class, but with different parameters such as number, type, or order of arguments.
Method Overriding is the process in which a subclass redefines a method of its superclass using the same method signature and compatible return type to provide a specific implementation.
These two mechanisms are forms of polymorphism:
Overloading
- is generally known as compile-time polymorphism.
Overriding
- is generally known as run-time polymorphism.
Main Content
1. Method Overloading
Meaning and purpose
Method overloading allows one method name to perform similar but slightly different tasks. It is useful when the operation is conceptually the same, but the input data varies. For example, a print() method may accept an integer, a string, or a floating-point value.
Rules and characteristics
In overloading, the method name must be the same, but the parameter list must differ. The difference can be in:
- number of parameters
- data types of parameters
- sequence/order of parameters
The return type alone cannot be used to distinguish overloaded methods. The compiler decides which version to call based on the arguments provided.
Example:
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
double add(double a, double b) {
return a + b;
}
}
Here, add() is overloaded three times. Each version handles different input forms.
2. Method Overriding
Meaning and purpose
Method overriding happens when a child class provides its own implementation of a method already inherited from the parent class. This is done when the subclass needs behavior that is more specific than the superclass version.
Rules and characteristics
The method name, parameters, and return type should match the parent method closely. The overriding method cannot reduce access level (for example, a public method in the parent cannot become private in the child). The @Override annotation is commonly used in languages like Java to make the intention clear and reduce errors.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
If Dog objects call sound(), the subclass version runs instead of the parent version.
3. Comparison Between Overloading and Overriding
Location and relationship
Overloading usually occurs within the same class, while overriding occurs between a parent class and a child class. Overloading does not require inheritance, but overriding depends on inheritance.
Binding and flexibility
Overloading is resolved at compile time, so it is less dynamic. Overriding is resolved at runtime, so it supports dynamic behavior based on the actual object type.
Simple visual comparison:
Method Overloading
Same class
|
+-- add(int, int)
+-- add(int, int, int)
+-- add(double, double)
Method Overriding
Parent class --> Child class
sound() --> sound()
Key distinction:
- Overloading = same name, different parameters
- Overriding = same signature, different class behavior
Working / Process
1. For method overloading, the compiler checks the method call.
It compares the method name and the number/type/order of arguments to select the best matching method. The decision is made before the program runs.
2. For method overriding, the runtime system checks the actual object.
When a parent reference points to a child object, the child’s version of the method is executed if it has overridden the parent method.
3. Polymorphic behavior is achieved through method selection.
Overloading lets one name handle multiple forms of input, while overriding lets subclasses customize inherited behavior without changing the method call syntax.
Example process for overriding:
Animal a = new Dog();
a.sound();
Even though the reference type is Animal, the actual object is Dog, so the Dog version of sound() executes.
Advantages / Applications
Improves readability and code simplicity
A single method name can be reused for related operations, making code easier to understand and remember.
Supports flexibility and extensibility
Overriding allows subclasses to extend or modify inherited behavior without rewriting the entire class design.
Widely used in real applications
Overloading is common in constructors, utility methods, and APIs. Overriding is widely used in frameworks, GUI programming, file handling, and system-defined class hierarchies.
Summary
- Method overloading uses the same method name with different parameters.
- Method overriding replaces a parent class method in a child class.
- Overloading is compile-time polymorphism, while overriding is run-time polymorphism.
- Important terms to remember: polymorphism, overloading, overriding, inheritance, compile-time binding, run-time binding