Below are the two important object-oriented concepts in Java:
- Method Overloading
- Method Overriding
These concepts allow us to use the same method name in different ways, which improves code readability, flexibility, and reusability.
1️⃣ Method Overloading
What is Method Overloading?
Method Overloading means defining multiple methods with the same name in the same class, but with different parameter lists.
Java decides which method to call at compile time, based on:
- Number of parameters
- Type of parameters
- Order of parameters
Example (From Class)
public class Bank {
public static void main(String[] args) {
Bank bank = new Bank();
bank.balance("Karthick", 101);
bank.balance(101);
bank.getInerest();
}
private void balance(int accountNo) {
System.out.println(accountNo);
}
public void balance(String name, int accountNo) {
System.out.println(name);
System.out.println(accountNo);
}
public void getInerest() {
System.out.println("5%");
}
}
Explanation
- Two methods have the same name:
balance -
But their parameters are different:
balance(int accountNo)balance(String name, int accountNo)
This is method overloading
When we call:
bank.balance("Karthick", 101);
Java calls:
balance(String name, int accountNo)
When we call:
bank.balance(101);
Java calls:
balance(int accountNo)
✔ The decision is made at compile time
Key Points of Method Overloading
- Happens within the same class
- Method name must be same
- Parameters must be different
- Return type alone cannot differentiate methods
- Also known as Compile-time Polymorphism (concept only, not started yet)
2️⃣ Method Overriding
What is Method Overriding?
Method Overriding occurs when a child class provides its own implementation of a method that already exists in the parent class, with:
- Same method name
- Same parameters
- Same return type
Inheritance is mandatory for method overriding.
Example (From Class)
public class SBI extends Bank {
public static void main(String[] args) {
SBI sbi = new SBI();
sbi.getInerest();
}
public void getInerest() {
System.out.println("10%");
}
}
Explanation
-
Bankclass has a method:
public void getInerest() {
System.out.println("5%");
}
-
SBIclass overrides the same method:
public void getInerest() {
System.out.println("10%");
}
- When we create an object of
SBI:
SBI sbi = new SBI();
sbi.getInerest();
✔ The child class method is executed
✔ Output:
10%
This happens because Java checks the object type at runtime
Key Rules of Method Overriding
- Inheritance is required
- Method signature must be the same
- Access modifier cannot be reduced
-
private,static, andfinalmethods cannot be overridden - Decision is made at runtime
Method Overloading vs Method Overriding
| Feature | Method Overloading | Method Overriding |
|---|---|---|
| Class | Same class | Parent–Child classes |
| Method name | Same | Same |
| Parameters | Different | Same |
| Inheritance | Not required | Required |
| Time of decision | Compile time | Runtime |
Real-Time Understanding
Method Overloading → Same action, different inputs
(Example: balance check by account number or by name + account number)Method Overriding → Same service, different behavior
(Example: Different banks offering different interest rates)
Interview Key Takeaways
- Method Overloading improves flexibility
- Method Overriding supports custom behavior
- Java resolves overloading at compile time
- Java resolves overriding at runtime
- Overriding always depends on inheritance
Top comments (0)