• Post Reply Bookmark Topic Watch Topic
  • New Topic

When is it required to use a Copy constructor & What necessitates overriding of Object Methods  RSS feed

 
zoheb hassan
Ranch Hand
Posts: 154
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am aware that a copy constructor is meant for creating a copy of an object but when and why do we have to use it, why not rather just create a new object, Basically i am trying to understand why we make that choice of using a copy constructor??

also

What necessitates overriding of hashCode(), toString() & equals() methods of Object class??


Please help me understand these questions
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A copy constructor is a convenient way to create an object that has already been initialized. With a copy constructor, you can instantiate and initialize an object with one statement. A typical usage would be with a class that has one or more immutable attributes whose values can only be set via a constructor.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always though a copy constructor is used to make an identical copy of an instance. And I thought it was necessary for a class with mutable attributes.
ExampleYou never actually require a copy constructor, but it is often a much nicer way to copy instances than clone();
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a class is immutable you never need copy its instances. Look what the API says about String. It can be shared.
 
zoheb hassan
Ranch Hand
Posts: 154
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, thanks for the replies, My Curiosity for copy constructor began when i took a look at the following example and never having used a copy constructor i got curious, I am posting the code below

I understand that a copy constructor helps in creating a identical copy of instance but why not rather create a new instance itself, What necessitates its use below is the code, I understand its a bean meant for setting and getting attributes but whats with the overriding of all those methods. This list basically gets put into a ArrayList


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
zoheb hassan wrote:I am aware that a copy constructor is meant for creating a copy of an object but when and why do we have to use it, why not rather just create a new object, Basically i am trying to understand why we make that choice of using a copy constructor??

I think that Junilu and Campbell have covered most of the 'what' and 'when', but more formally:

A copy constructor normally creates a new (ie, different) object that is "equal" to its source according to equals().

And it's often used as an alternative to implementing Cloneable, which has some issues of its own that you'll probably find out about later.

What necessitates overriding of hashCode(), toString() & equals() methods of Object class??

Simply put: the need to make an object behave properly. Simplest is probably to create a class of your own that doesn't override them and then create a couple of objects from it and print out the results of calling those methods for each one.

Strictly speaking, hashCode() is only required if you're likely to need to put objects in a "hashed" collection; but, since that's pretty well impossible to know when you're creating a class, the general rule of thumb is:
"override equals(), override hashCode(); don't override equals(), leave hashCode() out as well."

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope those methods were written by a machine
If I had to write that hash method I would try this
return Objects.hash(itemId, endTime, picUrl, smallPicUrl,
pic175Url, title, desc, dealUrl,
convertedCurrentPrice, primaryCategoryName,
location, quantity, quantitySold,
msrp, savingsRate, hot);

There is some poor design in that class, using Strings for a savings rate for example.
 
zoheb hassan
Ranch Hand
Posts: 154
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell Ritchie for pointing out the flaws in the code posted,

I am aware of basics of core Java but the higher specifics about design Thread management, Cache Management e.t.c., are all still a mystery to me, What can i do to get a good hold on these topics, I mean the static getInstance() method was not clear to me took some googling to find that it was a copy constructor.

I Mean i wish these topics were clear to me so it would help me in designing decent Android Applications

Thanks
Zoheb
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
zoheb hassan wrote:Thanks Campbell Ritchie . . .
You're welcome

. . . the static getInstance() method was not clear to me took some googling to find that it was a copy constructor.
. . .
Please tell us where you read that.

A static getInstance method is not a copy constructor. It is usually a factory method.
 
zoheb hassan
Ranch Hand
Posts: 154
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My bad Sheriff, misread it actually anyways apparently all these design patterns, factory methods e.t.c., are alien to me still have to learn a good deal about them,

Question is are there any good light resources for getting hold of these concepts
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
zoheb hassan wrote:Question is are there any good light resources for getting hold of these concepts

Not all of them, but this book covers most of what you've asked about; and it's excellent (the best "why to" book I've ever read about any language).

As for design patterns, there are several books out there, of which this is the "original". However, if you're still relatively new to programming, it might be a bit soon to be tackling design patterns. For starters, they are not Java-specific, so you may have a bit of "translation" to do; for seconds, you'll have plenty of stuff to learn in the next few months without having to worry about patterns.

HIH

Winston
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I always though a copy constructor is used to make an identical copy of an instance. And I thought it was necessary for a class with mutable attributes.


I suppose there may have been other ways but I've used copy constructors as a convenient way to partially initialize other objects from something that's already in memory. The case that comes to mind was a job configuration template. I had common job configuration parameters defined in the database, read those into memory, then just stamped out other job configurations from the "base" configuration using a copy constructor. The base configuration contained values for properties that were global and kept in the database and were immutable, while mutable properties were set on the fly and provided the means to customize each new job configuration. This allowed me to have code like so:

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that seems a good alternative use for it. Not seen that before.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!