What is the difference between overloading a method and overriding a method? Can anyone explain it with an example?
2 Answers
Method overloading deals with the notion of having two or more methods in the same class with the same name but different arguments.
void foo(int a)
void foo(int a, float b)
Method overriding means having two methods with the same arguments, but different implementations. One of them would exist in the parent class, while another will be in the derived, or child class. The @Override
annotation, while not required, can be helpful to enforce proper overriding of a method at compile time.
class Parent {
void foo(double d) {
// do something
}
}
class Child extends Parent {
@Override
void foo(double d){
// this method is overridden.
}
}

- 1
- 1

- 8,558
- 10
- 54
- 79
-
30`@Override` is not required. It's a good practice, but not required. – GriffeyDog Sep 11 '12 at 19:07
-
7Sorry @GriffeyDog. You are right "@Override" is not compulsory.But to remember that you are overriding a method, it is a good practice..Thanks for reminding.. – Hisham Muneer Sep 11 '12 at 21:24
-
5Overloading need not be in the same class but can be also be done between parent class and derived class. – happs Jan 07 '14 at 21:23
-
1Re @happs's comment: It's still overloading if a parent class defines one signature, and a derived class defines a second signature, per [JLS§8.4.9](https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.9): *"If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded."* – T.J. Crowder Jun 24 '15 at 07:17
Method overriding is when a child class redefines the same method as a parent class, with the same parameters. For example, the standard Java class java.util.LinkedHashSet
extends java.util.HashSet
. The method add()
is overridden in LinkedHashSet
. If you have a variable that is of type HashSet
, and you call its add()
method, it will call the appropriate implementation of add()
, based on whether it is a HashSet
or a LinkedHashSet
. This is called polymorphism.
Method overloading is defining several methods in the same class, that accept different numbers and types of parameters. In this case, the actual method called is decided at compile-time, based on the number and types of arguments. For instance, the method System.out.println()
is overloaded, so that you can pass ints as well as Strings, and it will call a different version of the method.

- 24,713
- 30
- 122
- 169
-
4If you have a child class that defines the same method with different parameters then is that considered to be both overriding and overloading? – barlop Apr 21 '14 at 22:55
-
4then it's overloading only as it's not overriding any of the parent method. – Vincent Zou May 16 '15 at 14:18
-
To add on that, the next question would be which method to execute? , the summary is :in overloading we follow the reference type in execution, in overriding we follow the object type in execution. – Naim R. Apr 24 '22 at 09:49