• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Overriding Fragments

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Following is a problem form CertPal


Given the following classes what can be inserted in the code fragment shown ?


public Integer hello(){ return 0;} //1

Why line 1 is not a correct answer, i think Boxing is allowed in this case.

Thanks in advance
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int return type is not same as Integer return type. there is no concept of boxing/unboxing in method signature. the boxing/unboxing happens when ever you work with operator, and return a value
 
Greenhorn
Posts: 18
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

public Integer hello(){ return 0;} //1


So, what is really wrong with the code? in still runs fine. I still dont get it. Hoping for a more detailed explanation. thanks.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java method overriding rules :

* We can not override final methods.
* Methods declared as private cannot be overridden as they are not visible outside the class.
* Access modifier must be same or less restrictive. For example if super class contains protected you can have public or protected.
* The return type must be same, or it may be co variant return (sub class)
* Must have the same argument list
* We can throw any unchecked exceptions
* We can throw fewer or narrowed checked exceptions

So, you can't return a boxing/unboxing values.
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic