What is the difference between overloading and overriding?
Method overloading means having several methods with the same name but different parameters in the same class, and it is decided at compile time. Method overriding means a child class provides its own version of a method it inherited from a parent, and it is decided at run time. Overloading is about many forms of one method name, overriding is about replacing inherited behaviour.
- Overloading means several methods with the same name but different parameters in one class, resolved at compile time.
- Overriding means a child class replaces an inherited method with its own version, resolved at run time.
- Overloading adds flexible variations, while overriding changes inherited behaviour.
Overloading: same name, different inputs
Overloading lets you use one friendly method name for related actions that take different inputs. The compiler picks the right one based on the arguments you pass. This happens in a single class.
class Calculator {
public:
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
};class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
}# Python has no true overloading.
# Default and variable arguments give the same effect.
class Calculator:
def add(self, a, b, c=0):
return a + b + cOverriding: child replaces parent behaviour
Overriding happens across two classes linked by inheritance. The child keeps the same method name and parameters as the parent but gives its own body. At run time the program uses the child version.
class Animal {
public:
virtual void sound() { cout << "some sound"; }
};
class Cat : public Animal {
public:
void sound() override { cout << "meow"; }
};
Animal* a = new Cat();
a->sound(); // prints meowclass Animal {
void sound() { System.out.println("some sound"); }
}
class Cat extends Animal {
@Override
void sound() { System.out.println("meow"); }
}
Animal a = new Cat();
a.sound(); // prints meowclass Animal:
def sound(self):
print("some sound")
class Cat(Animal):
def sound(self): # override
print("meow")
a = Cat()
a.sound() # prints meowQuick comparison
- Where: overloading is in one class, overriding is across parent and child classes.
- Parameters: overloading needs different parameters, overriding needs the same parameters.
- Timing: overloading is resolved at compile time, overriding is resolved at run time.
- Goal: overloading adds flexible variations, overriding changes inherited behaviour.
A favourite follow up is whether you can override a static method. The answer is no, that is called method hiding, not overriding, because static methods belong to the class and are resolved at compile time. Mentioning this shows depth.
Frequently asked questions
Can you override a static method in Java?
No. Redefining a static method in a child class is called method hiding, not overriding, because static methods are resolved at compile time by class.
Can you overload the main method?
Yes, you can define other main methods with different parameters, but the Java Virtual Machine only calls the one that takes a String array.
Common follow up questions
Related interview questions
Want the full OOP guide?
Read every OOP concept with notes, diagrams, and code in one place. Track your progress as you go.
Open the OOP guide All OOP questions