Understanding overloading and overriding in java is a cornerstone of object-oriented programming. Method overloading occurs when multiple methods in the same class have the same name but different parameters (compile-time polymorphism). In contrast, overriding happens when a subclass provides a specific implementation for a method already defined in its superclass (run-time polymorphism). Overloading is about versatility within a class, while overriding is strictly tied to inheritance.
Method overriding means a subclass provides its own implementation of a method that already exists in its parent class – same name, same parameters, same return type. The JVM decides which version to call at runtime based on the actual object type – this is runtime polymorphism. Overriding is only possible through inheritance.
What Is Method Overloading in Java?
Overloading allows a class to have multiple methods sharing the same name, as long as their parameter lists differ – either in the number of parameters, the types of parameters, or both. The return type alone cannot distinguish overloaded methods.
Think of it like a restaurant with a ‘serve’ function: serve(pizza), serve(pasta), serve(salad) all do the same general action but handle different inputs differently. The system picks the right one based on what you pass in.
What Is Method Overriding in Java?
Overriding happens when a child class redefines a method inherited from its parent. The overriding method must have the exact same signature – same name, same parameter types, and same (or covariant) return type. It cannot have a more restrictive access modifier than the parent method.
Think of it like this: a parent class Animal has a method makeSound(). The child class Dog overrides makeSound() to return ‘Woof’ instead of a generic sound. Same method name, different behaviour – determined at runtime by the actual object type.
Overloading vs. Overriding – Key Differences
| Feature | Method Overloading | Method Overriding |
| Definition | Same name, different parameters | Same name, same parameters |
| Where it happens | Same class | Parent and child class |
| Polymorphism type | Compile-time (static) | Runtime (dynamic) |
| Inheritance required? | No | Yes |
| Return type | Can differ | Must be same or covariant |
| Access modifier | Can be anything | Cannot be more restrictive |
| Static methods | Can be overloaded | Cannot be overridden |
| @Override annotation | Not used | Recommended to use |
Method Overloading – Code Example
Here is a simple Java class demonstrating method overloading:
public class Calculator {
// Method 1: adds two integers
public int add(int a, int b) {
return a + b;
}
// Method 2: adds three integers (different number of params)
public int add(int a, int b, int c) {
return a + b + c;
}
// Method 3: adds two doubles (different parameter type)
public double add(double a, double b) {
return a + b;
}
}
In this example, all three methods are named ‘add’ but the compiler knows which one to call based on the arguments passed. add(2, 3) calls method 1. add(2, 3, 4) calls method 2. add(2.5, 3.5) calls method 3. Clean, intuitive, and all resolved before the program runs.
Method Overriding – Code Example
Here is a Java example showing overriding in action:
public class Animal {
public void makeSound() {
System.out.println(“Some generic animal sound”);
}
}
public class Dog extends Animal {
@Override
public void makeSound() {
System.out.println(“Woof!”);
}
}
public class Cat extends Animal {
@Override
public void makeSound() {
System.out.println(“Meow!”);
}
}
When you call makeSound() on an Animal reference that holds a Dog object, Java calls the Dog version at runtime – not the Animal version. This is the power of runtime polymorphism: the actual behaviour depends on what the object really is, not what variable type holds it.
Rules to Remember
Rules for Overloading
Parameters must differ – either in number, type, or order. Return type alone is not enough to distinguish overloaded methods. Access modifiers can be different across overloaded versions. Static, final, and private methods can all be overloaded.
Rules for Overriding
The method signature must be identical to the parent method. The access modifier cannot be more restrictive (public parent method cannot be overridden as private). The overriding method can throw fewer or narrower checked exceptions, but not broader ones. Use the @Override annotation always – it catches mistakes at compile time. Static methods cannot be overridden – they can be hidden, which is a different concept.
Real-World Use Cases
| Use Case | Overloading or Overriding? | Example |
| Printing different data types | Overloading | print(int), print(String), print(double) |
| Animal hierarchy sounds | Overriding | Dog.makeSound() vs Cat.makeSound() |
| Constructor variations | Overloading | Person(), Person(name), Person(name, age) |
| Payment processing types | Overriding | CreditCard.process() vs PayPal.process() |
| Math utility functions | Overloading | Math.abs(int), Math.abs(double) |
| UI component rendering | Overriding | Button.draw() vs ImageView.draw() |
Common Mistakes Beginners Make
Thinking return type alone creates an overload: It does not. Java will throw a compile error if two methods have the same name and parameters but different return types.
Forgetting @Override: Without the annotation, if you misspell the method name in a subclass, Java will silently treat it as a new method – not an override. The @Override annotation catches this immediately.
Trying to override static methods: Static methods belong to the class, not the instance. You can define a static method with the same name in a subclass, but this is method hiding, not overriding – behaviour is very different.
Confusing the two concepts in interviews: A very common interview trap. Remember – overloading is same class, compile-time, different parameters. Overriding is parent-child, runtime, same parameters.
Frequently Asked Questions
Can we overload and override the same method?
Not the same method simultaneously – but a class can have overloaded methods, and those individual methods can each be overridden by subclasses separately.
Can constructors be overridden?
No. Constructors cannot be overridden because they are not inherited. However, constructors can be overloaded – and this is extremely common in Java.
What happens if we override a method incorrectly?
Without @Override, Java may simply treat your method as a new method in the subclass rather than an override, silently breaking your intended polymorphic behaviour. With @Override, the compiler catches the mistake immediately.

