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.
Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Hey Kumar! Seems today is the day of "Overriding and Overloading"
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
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<br />SCJP 1.4 (85%)
Don't get me started about those stupid light bulbs. |