Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic