• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding Doubt

 
Ranch Hand
Posts: 463
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Here is my programme.

<code>
class Parent {
public int doStuff(float y) {
return 4;
}
}

public class OverridingTest extends Parent {
public Integer doStuff(float y) {
return 2;
}

public static void main(String[] args) {

}
}
</code>

After compiling this I am getting the following error.

OverridingTest.java:8: doStuff(float) in OverridingTest cannot override doStuff(
float) in Parent; attempting to use incompatible return type
found : java.lang.Integer
required: int
public Integer doStuff(float y) {
^
1 error

My doubt is, when the return type is Integer I should be able to return int primitive, I mean, auto boxing should work here. Why this code is not getting compiled?

Thanks & Regards,
Surya.
[ November 12, 2006: Message edited by: Sai Surya ]
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The override is not following valid overriding rule the return type should be same or should be covariant return type(you can refer to the K&B book for the rules).
To make it compilable either change the return type of either methods equals to other ones return type.
OR(Covariant returns)
 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sanjeev!

Howerver, my doubt is, why auto boxing NOT taking place when I return int value for Integer return type?

- Surya.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why auto boxing NOT taking place when I return int value for Integer return type?


The Autoboxing takes places but the overridding method is not a valid one (does not sticks to the rules of legal overrides).
reply
    Bookmark Topic Watch Topic
  • New Topic