aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Query on ReturnType of Overriding method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Query on ReturnType of Overriding method" Watch "Query on ReturnType of Overriding method" New topic
Author

Query on ReturnType of Overriding method

vikram hegde
Greenhorn

Joined: Sep 07, 2005
Posts: 18
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

Joined: Jul 04, 2004
Posts: 979
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

Joined: Jun 22, 2005
Posts: 1527
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???


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Sowjanya Chowdary
Ranch Hand

Joined: Aug 22, 2005
Posts: 35
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

Joined: Jun 22, 2005
Posts: 1527
Hey Kumar! Seems today is the day of "Overriding and Overloading"
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi Akhil...



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



You are right...buddy
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
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.


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
here how come the return types are different can any one explain plz


Harish<br />SCJP 1.4 (85%)
Ronnie Ho
Ranch Hand

Joined: Aug 10, 2005
Posts: 47
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Query on ReturnType of Overriding method