What is the difference between method overloading and overriding?
Solution 1
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.
}
}
Solution 2
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.
Related videos on Youtube
Makoto
Stack Overflow Inc. is treating the community which built it up as a dispensable tool in its quest to maintain relevance and market share. While I have no qualms with the network needing to make money, delivering a "product" and then asking the community to "add value" to it is not the path forward that I seek. In light of leadership's hard-line stance to this, the only form of civil disobedience we have at our disposal is to simply cease all moderation, which is the only real value-add we normal users of the site have brought.
Updated on July 09, 2022Comments
-
Makoto almost 2 years
What is the difference between overloading a method and overriding a method? Can anyone explain it with an example?
-
GriffeyDog over 11 years
@Override
is not required. It's a good practice, but not required. -
Hisham Muneer over 11 yearsSorry @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..
-
happs over 10 yearsOverloading need not be in the same class but can be also be done between parent class and derived class.
-
barlop about 10 yearsIf you have a child class that defines the same method with different parameters then is that considered to be both overriding and overloading?
-
Vincent Zou almost 9 yearsthen it's overloading only as it's not overriding any of the parent method.
-
T.J. Crowder almost 9 yearsRe @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: "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."
-
Naim R. about 2 yearsTo 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.