Reputation: 198338
Java code:
class P {
public void hello() {}
}
class C extends P {
public void hello(String s) {}
}
My question is: Is the hello
in class C
overloading the one with same name in super class P
?
My friend says they are not because the are not in the same class.
Upvotes: 11
Views: 44423
Reputation: 3752
This is a good question and the answer is a bit tricky.
Well, it's true that you can overload an inherited method from a parent class into a subclass. However, and here's the interesting part, the actual behavior depends on the reference variable type.
Let's consider the following example:
public class OverLoadingTest {
public static void main(String[] args) {
ChildClass cc = new ChildClass();
SuperClass sc = cc;
sc.method("lol");
cc.method("lol");
}
static class SuperClass {
public void method(Object o) {
System.out.println("SuperClass called.");
}
}
static class ChildClass extends SuperClass {
public void method(String s) {
System.out.println("ChildClass called.");
}
}
}
So, we have a class extending another and with a method that overloads a method from the parent.
It's easy to guess that if you have an instance of ChildClass
, the two methods are overloaded, and overloading resolution takes place as it normally does.
However, let's consider creating an instance of ChildClass
and assigning it to a reference variable of type SuperClass
. Is the overloading thing still standing?
If you execute this program you will get this outptut:
SuperClass called.
ChildClass called.
The output clearly indicates that there's no overloading here in this case. This however can be altered by overriding the original method.
static class ChildClass extends SuperClass {
public void method(String s) {
System.out.println("ChildClass called.");
}
public void method(Object o) {
System.out.println("ChildClass called.");
}
}
Now, if you run the program again, you get this output:
ChildClass called.
ChildClass called.
Now, why is JVM behaving that way? Why can't it see the overloading method as we're using an instance of the child class?
This takes us to how does JVM call a method. The JVM sees that you're referring to the object with a reference of type SuperClass
, so, it can only use the methods that are related to that type, with the only exception is overriden methods. And since method(String)
isn't overriding, we have method(Object)
of the parent, hence, it's the one chosen for execution.
We then override the method to break this rule, and this is how the JVM called ChildClass.method(Object)
even if the reference variable is of a parent class.
Upvotes: 1
Reputation: 674
Depends on the class. From class P's perspective (if the reference is P, the object can be of C) it is not. If you write something like: P p = new C();
there is no overloading because you cannot call p.hello("foo")
.
From class C's perspective it is overloaded because if you write C c = new C();
it has two methods with same name and different signatures.
Upvotes: 0
Reputation: 550
Source of confusion: Your friend would be right if speaking about C++ not Java. In C++, function overloading can only occur between members of the same class. Whereas in Java, overloading can occur, in addition to that, across two classes with inheritance relationship.
Upvotes: 1
Reputation: 339
Yes we can overload the super class method in sub class like as bellow:
public class OverLoading {
public static void main(String[] args) {
B b = new B();
b.display();
b.display(4);
}
}
class A {
public void display() {
System.out.println("A class display method");
}
}
class B extends A {
public void display() {
System.out.println("class B subclass");
}
public void display(int a) { //Overloading in subclass
System.out.println("class B subclass with overloading");
}
}
Output:
class B subclass
class B subclass with overloading
Upvotes: 1
Reputation: 41
Good question!!!In sub class if method name | parameter type | list is changed then sub class method will not be considered as overriding it is considered as overloading method Example :
class A{
void m1(int a){}
}
class B extends A{
void m1(float f)
{}
}
In above program m1 method is a overloaded method.
Upvotes: 0
Reputation: 21
Yes, your friend is wrong because he thinks only of the concept of overriding.
But here hello()
, and hello(String s)
are different by there parameters so it's overloading not overriding.
Upvotes: 2
Reputation: 348
Simple Explanation:
I think this question arises because at times we hear the following,
"Method overloading is performed within class. Method overriding occurs in two classes that have inheritance relationship."
The above statement is correct. But your friend is wrong. why?
Because when you extend a class, the subclass have all the methods defined by superclass. It is as if all the methods of superclass have been implemented by the subclass. That means the hello() method has been implemented by the class C as well. Now, you added a method in class C with different parameter (hello(String s)). That means, class C has two methods in all with same name but different parameters and that is "overloading".
Hope it is crystal clear.
Upvotes: 9
Reputation: 11940
It is a valid question since usually, overloading is explained using two methods with the same name (but different parameters) in the same class.
I would argue that yes, the method hello
in C
is overloading P
's hello
method because of the "is a" relation.
The "is a" relation states that since C subclasses P, it is also an instance of P ("C is a P"). Hence C has 2 overloaded hello
-methods.
Upvotes: 0
Reputation: 7894
Taking a more formal approach, the Java Language Specification for Java 7 states:
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.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.9
I would point your friend to this link.
So, in short, in your example, the hello method is indeed overloaded.
Upvotes: 20
Reputation: 742
Overloading can happen in same class as well as parent-child class relationship whereas overriding happens only in an inheritance relationship.
Upvotes: 2
Reputation: 22196
Long story short, an instance of C
will have both the hello()
and the hello(String s)
methods available. An instance of P
will only have the hello
method available.
This is indeed overloading, as you have two methods of the same name taking different parameters.
However, it is not overriding, because overriding is having a method declared in a subclass with the same name and same parameters as a method in a superclass.
E.g. if you had
class C extends P {
public void hello() {}
}
it would be overriding the hello()
method declared in P
. When invoking new C().hello()
in that case, you would invoke the implementation of the hello()
method declared in class C
.
Upvotes: 0
Reputation: 95998
Overloading is when you have two methods that have the same name but different signatures (Your case).
Side note: Overriding is when you have two methods that have exactly the same signature and name and the parent class.
Upvotes: -1
Reputation: 4630
Yes it is overloading
, This overloading is happening in case of the class 'C
' which is extending P
and hence having two methods with the same nam
e but different parameters
leading to overloading of method hello()
in Class C
. However Class P
is only able to access one of the methods which is present in its own definition.
Upvotes: 0