Overloading vs Overriding

overloading-and-overriding

Continuing with the blog posts about the mastery topics, we get to Overloading and Overriding. These two concepts are related to the use of methods in classes, but they differ in many things.

Overloading is the action of using two methods with THE SAME NAME & AND IN THE SAME CLASS, but the ARGUMENTS & THE RETURN TYPE are different. For example we have this piece of code:

public class Overload {

public int sum(int x, int y) {
return x + y;

}

public double sum(double x, double y, double z) {
return x + y + z;

}

We have a couple of methods in the Overload class, both called ‘sum’, but the first has a return type ‘int’, and receive two variables ‘int’; while the second one has a return type ‘double’, and receives three variables ‘double’. In Overloading, Inheritance and Polymorphism are not involve.

The we have Overriding, that is some kind the opposite of Overloading. Overriding occurs when you have a SupperClass  with a method: int multiply(). Then you have a SubClass with another method called ‘multiply’. For it to be an Overriding, the method of the subclass MUST have the same ARGUMENT & same RETURN TYPE. In this case inheritance and polymorphism are clearly involved in this action.

All this info was provided by a Youtube video posted by EJ MEDIA. Here it is:

 

Un comentario en “Overloading vs Overriding”

Deja un comentario