Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarification on overloading

When the parent class have an add method with 2 parameters, If we add new add method with 3 parameters in child class, shall we call this as over loading?

Thanks in advance.

like image 354
Vaandu Avatar asked Aug 18 '11 09:08

Vaandu


4 Answers

Yes, since the method with two parameters is inherited by the subclass, the method with three parameters is said to be an overloading method.

class A
    add(param1, param2)

class B
    add(param1, param2)            <-- inherited
    add(param1, param2, param3)    <-- overloading the above method

A quote from the official trail on Overriding and Hiding Methods:

Note: In a subclass, you can overload the methods inherited from the superclass. Such overloaded methods neither hide nor override the superclass methods—they are new methods, unique to the subclass.

(As you probably already figured out, the method with three classes is not an overriding method.)

like image 111
aioobe Avatar answered Oct 13 '22 01:10

aioobe


Yes, this is overloading. It would be overloading even if the method were in the same class as the method with two parameters.

Note that when there are different numbers of parameters (and no varargs parameters) overloading is reasonably simple. It gets a lot more complicated when you have methods with the same number of parameters - at that point the compiler has to choose the "best" method out of applicable candidate methods.

Also note that overloading is determined at compile time whereas which override is executed is determined at execution time based on the actual type of the object the method is called on.

like image 21
Jon Skeet Avatar answered Oct 13 '22 00:10

Jon Skeet


Overloading is when methods have the SAME NAME but DIFFERENT SIGNATURE. Overriding - when methods have IDENTICAL NAMES and IDENTICAL SIGNATURE.

like image 45
LordDoskias Avatar answered Oct 13 '22 01:10

LordDoskias


Yes definitely a overloading and a nice feature of inheritance.

like image 23
Android Killer Avatar answered Oct 12 '22 23:10

Android Killer