• Post Reply Bookmark Topic Watch Topic
  • New Topic

Temporary object of an abstract class  RSS feed

 
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add this line at the end of your main(...) method:What do you get?
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Double is a final class that extends the Number see this : webpage

Satya
 
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

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

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!