Reputation: 60
Consider the following piece of code
public class SuperClass
{
//label 1
void function(int a)
{
System.out.println(a);
}
}
public class SubClass extends SuperClass
{
//label 2
void function(int a)
{
a++;
System.out.println(a);
}
//label 3
void function(float a)
{
System.out.println(a);
}
}
From what I have learnt, label 2 is an example of function overriding because a superclass method is being re-written in the sub class with its own implementation, while label 3 is a form of function overloading because the parameters are different while the function name is same.
My question is if label 3 is in fact function overloading, which function is it overloading? Label 1 or label 2?
My theory is that label 3 is overloading label 2 because they are part of the same class.
I have come across these links -
Does overloading work with Inheritance?
Different ways of Method Overloading in Java
while they provide useful information, they don't really answer my question.
This is only a conceptual question I had while learning polymorphism in Java. Any help would be appreciated.
Upvotes: 2
Views: 131
Reputation: 4279
Overloading occurs within a class, so, your assumption is correct.
label 2 and label 3 are overloaded
Upvotes: 1
Reputation: 1074285
My question is if label 3 is in fact function overloading, which function is it overloading? Label 1 or label 2?
Label 2, because the overloading only appears within SubClass
. There is no overload in SuperClass
. (Although you could argue that since label 2 overrides label 1, the answer is sort of "both," but in any context where the overload actually occurs, you'd have to have the override — e.g., you'd have to be dealing with a SubClass
reference — so really it's just label 2.)
Upvotes: 3