Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Constructor Overloading-

 
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Sybex book, Under constructor overloading it says

1)

This attempt does compile. It doesn't do what we want, though. When the constructor
with one parameter is called, it creates an object with the default weight and color. It then
constructs a different object with the desired weight and color and ignores the new object.
That's not what we want. We want weight and color set on the object we are trying to
instantiate in the first place.



I have a confusion in  understanding the concept here. I believe above quote is in reference with the code attached. If I am right then my confusion is  in single parameter constructor.Here, we
are passing only weight parameter then how is an object is created with default weight and color ? and also when they say new object is ignored  do they mean first created object by single parameter constructor ?

I doubt that code I am referring is wrong or my understanding about objects. This might seem silly doubt but it would help my understanding.


 
Rancher
Posts: 4174
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the point of view of the object being created using the 'weight' constructor, the constructor may as well be:

As the extra Hamster object created using 'new' is not stored anywhere.
As you can see, the 'weight' parameter is not being used for this Hamster, so the values for the attributes will stay at their default.

To take the whole constructor, as in your post, if I create a Hamster as follows:

I would end up with a Hamster with default values (as above), and while it was creating that Hamster, it would create an extra Hamster inside the constructor, whose reference is never retained and so will be eligible for garbage collection straight away.

So there are two Hamsters involved here.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This topic has an excellent explanation about the same code snippet. Definitely worth reading!
 
Vidya Shivram
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Dave -Thanks, Dave it helped
@Roeol -Indeed, it was an excellent explanation. Thank you
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Chivid Ram, you may want to look into constructor chaining using the

and




I think what you really want is



super(); comes into play when you are inheriting/driving classes.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!