• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Temporary object of an abstract class

 
Ranch Hand
Posts: 139
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am under the assumption that In the return statement of getReciprocal() method(of the following code), a temporary Number object is created to hold the result of the calculation.

My question is, Number is an abstract class and we are only able to create reference of an abstract class not an object. But then how a temp Number object is created and returned?

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add this line at the end of your main(...) method:What do you get?
 
Quazi Irfan
Ranch Hand
Posts: 139
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:What do you get?



I get java.lang.Double.

So return statement can create an temp object, because Double is an concrete implementation of Number?
 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Double is a final class that extends the Number see this : webpage

Satya
 
Sheriff
Posts: 27235
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Quazi Irfan wrote:So return statement can create an temp object, because Double is an concrete implementation of Number?



I'm not sure what you mean by a "temp" object. But anyway it's not the return statement which is creating that Double object. It's the toDouble() method of whatever object your "ob" variable refers to.

 
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well like Paul said, im not sure "too" what you mean by a temp object.. but i'm trying to answer your question..

Number is an abstract class and we are only able to create reference of an abstract class not an object. But then how a temp Number object is created and returned



"of course" number is an abstract class, thus we're not able to create a "Number" object, but then how a temp Number object is "created" and "returned"?
well actually there's no "Number" object created on your code..



so the object always "Double" which is subclass of the Number class (so it was meet the requirement of the Generic type that you declare -> <Double extends Number>) ..
then what about the getter?



the method never create any "Number" object, but the method was "declared" to return any of "Number" object -> then.. the return statement was a "Double" object (and i'm not sure the doubleValue() was necessary).. then why compiler let the Double object was returned? but AHA we should remember that Double "IS-A" Number right? since the Double is a subclass of Number..

hope this help.. cheers
 
Quazi Irfan
Ranch Hand
Posts: 139
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.

If I don't use doubleValue() method I get the following error,

 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic