Define Method Overloading and Method Overriding with examples
Method overloading and method overriding are two important concepts in object-oriented programming (OOP), especially in languages like…
Define Method Overloading and Method Overriding with examples
Method overloading and method overriding are two important concepts in object-oriented programming (OOP), especially in languages like Java, C++, and Python. They both involve using the same method name, but they differ in how and why they are used.
🔹 Method Overloading
✅ Definition
Method overloading means defining multiple methods with the same name in the same class, but with different parameters (different number, type, or order of parameters).
👉 It is an example of compile-time polymorphism.
✅ Key Points
- Same method name
- Different parameter list
- Happens within the same class
- Return type alone is not enough to differentiate methods
💻 Example (Java)
class Calculator {
// Method with 2 parameters
int add(int a, int b) {
return a + b;
}
// Method with 3 parameters
int add(int a, int b, int c) {
return a + b + c;
}
// Method with different data type
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator obj = new Calculator();
System.out.println(obj.add(2, 3)); // calls first method
System.out.println(obj.add(2, 3, 4)); // calls second method
System.out.println(obj.add(2.5, 3.5)); // calls third method
}
}
🔹 Method Overriding
✅ Definition
Method overriding means redefining a method in a subclass that already exists in the parent class, using the same method signature.
👉 It is an example of runtime polymorphism.
✅ Key Points
- Requires inheritance
- Same method name and parameters
- Method in child class replaces parent version
- Decided at runtime
💻 Example (Java)
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
// Overriding method
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal obj = new Dog(); // Parent reference, child object
obj.sound(); // calls Dog's method
}
}
🔁 Difference Between Overloading and Overriding
Feature Method Overloading Method Overriding Definition Same method name, different parameters Same method, same parameters Inheritance Not required Required Polymorphism Type Compile-time Runtime Class Same class Parent & child class Exampleadd(int, int) vs add(int, int, int)Parent sound() → Child sound()
✔️ In short:
- Overloading = same method name, different inputs
- Overriding = same method, different behavior in child class
메타데이터
- post_id
- 8b90f5e667e7
- slug
- define-method-overloading-and-method-overriding-with-examples-8b90f5e667e7
- url
- https://medium.com/@Dilip-Adhikari/define-method-overloading-and-method-overriding-with-examples-8b90f5e667e7
- canonical_url
- https://medium.com/@Dilip-Adhikari/define-method-overloading-and-method-overriding-with-examples-8b90f5e667e7
- author_url
- https://medium.com/@Dilip-Adhikari
- status
- ok
- fetched_at
- 2026-06-13 00:08:42