This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query on ReturnType of Overriding method

 
vikram hegde
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can any one explain, why the below code won't complie

File B.java
------------
class A{
public A getNewInstance(){
return new A();
}
}

public class B extends A{
public B getNewInstance(){
return new B();
}
}

At complie time we get the below exception,
B.java:8: getNewInstance() in B cannot override getNewInstance() in A; attempting to use incompatible return type
found : B
required: A
public B getNewInstance(){
^
1 error

I am not getting why is this so happening? Since the return type of method getNewInstance()of class B is returning object of Classtype B and since Class B has an *is a* relationship with Class A, as it is extending from Class A. Then, Why is this complie time exception is thrown?

Example taken above is return on my own, much better example to explain is appreciated.

Please excuse if the question seems silly. but please do clarify

Thanks in Advance,
Vikram
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java 1.4 ...


When you are overriding ...a method the return type should be the same as is defined for the method in the parent class..

If you happen to have a subclass of the parent class method return type as the return type for the over riding method..it would flag an error..

i.e






From the JLS..




8.4.6.3 Requirements in Overriding and Hiding
If a method declaration overrides or hides the declaration of another method, then a compile-time error occurs if they have different return types or if one has areturn type and the other is void. Moreover, a method declaration must not have a throws clause that conflicts (�8.4.4) with that of any method that it overrides or hides; otherwise, a compile-time error occurs.





[ October 03, 2005: Message edited by: A Kumar ]
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummmm.... parent class and child class ok, the child class gets the copy of the non-private or public method "getInstance()". So in the bag of child class B there may some stuff of this kind...



getNewInstance
getNewInstance! !! Here is the alarm

There are two methods with same names... it is not overriding because the "return type" AND "argument list" of overriding method must be same as overridden method.

Looking at the signature we can not even call it overloading, hence it is raising compile time errors...

I think compiler wants to know which method should I call when you invoke getNewInstance() with a child class i.e. B's reference???
 
Sowjanya Chowdary
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi vikram,

To my knowledge, to override a method the signature(which includes method name and parameters) and return types must me same.
Here return types for getNewInstance() methods in Class A and Class B are different.
It is not a valid overriding.

This is showing complie time exception because it can't be even considered overloading. Since to overload a method the parameters must be different.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Kumar! Seems today is the day of "Overriding and Overloading"
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Akhil...



Hey Kumar! Seems today is the day of "Overriding and Overloading"



You are right...buddy
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vikram vishwanath:
Hi All,

Can any one explain, why the below code won't complie

File B.java
------------
class A{
public A getNewInstance(){
return new A();
}
}

public class B extends A{
public B getNewInstance(){
return new B();
}
}

At complie time we get the below exception,
B.java:8: getNewInstance() in B cannot override getNewInstance() in A; attempting to use incompatible return type
found : B
required: A
public B getNewInstance(){
^
1 error

I am not getting why is this so happening? Since the return type of method getNewInstance()of class B is returning object of Classtype B and since Class B has an *is a* relationship with Class A, as it is extending from Class A. Then, Why is this complie time exception is thrown?

Example taken above is return on my own, much better example to explain is appreciated.

Please excuse if the question seems silly. but please do clarify

Thanks in Advance,
Vikram


Hi,
The above program would compile fine in JDK 1.5.
Since in JDK1.5 the overriding method may return same or a subclass of the object returned by the overridden method.
 
harish shankarnarayan
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here how come the return types are different can any one explain plz
 
Ronnie Ho
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harish,

To override a method in 1.4, the java compiler requires two methods to have the exact same signature.

even if B extends A. That's why in 1.4, you'll get compiler error. In 5.0, the compiler allows the return type of the subclass's method to be the child of the superclass's method return type. Hope this helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic