• Post Reply Bookmark Topic Watch Topic
  • New Topic

do a deep clone on children  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'd like to know if there's a way to have an abstract class perform a deep copy of itself via its "clone()" method, even for those fields that are declared by the abstract class's sons.

I don't want the abstract class's sons to implement the clone() method themselves, since there's a fair amount of processing I'd like to do inside clone after the objects are 'cloned' that should be common to any implementations.


Thanks for your time,

Beto.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Don't say "parent" and "child," say "superclass" and "subclass."

If there are fields in the subclasses which are not declared in the superclass, no. The superclass cannot have access to members of its subclasses.
 
Alberto Caraz
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! Thanks for your prompt reply.

a) I am unsure as to why those terms are more appropriate than parent an child. I think both terms are pretty standard in the literature.

Even the Java tutorial itself uses both terms:
http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html

b) You can actually 'see' them by using reflection, essentially replicating Object's clone() mechanism. To counter your claim, it just suffices to see that Object manages to make a field by field referential copy of the elements of its superclass to realize that it is having 'access' to them.

My question is more oriented towards what kind of good design I could use to prevent myself from using that, which looks like a hack.

Thanks a lot
 
Alberto Caraz
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
er.. I meant manages to make a referencial copy of the subclass. sorry about that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!