Skip to main content

#TC201 #Topic14 What are overloading and overwriting? How do we implement those in Java?

2 min read

Method overloading in Java occurs when two or more methods in the same class have the exact same name but different parameters, is a feature that allows a class to have two or more methods having same name, if their argument lists are different.

Declaring a method in subclass which is already present in parent class is known as method overriding. The main advantage of method overriding is that the class can give its own specific implementation to a inherited method without even modifying the parent class. The benefit of overriding is: ability to define a behaviour that's specific to the subclass type which means a subclass can implement a parent class method based on its requirement.

Let's summarize the differences between overloading and overriding. When overloading, one must change either the type or the number of parameters for a method that belongs to the same class. Overriding means that a method inherited from a parent class will be changed. But, when overriding a method everything remains exactly the same except the method definition basically what the method does is changed slightly to fit in with the needs of the child class. But, the method name, the number and types of parameters, and the return type will all remain the same.