This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Please help - cloaning members within a superclass and class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please help - cloaning members within a superclass and class." Watch "Please help - cloaning members within a superclass and class." New topic
Author

Please help - cloaning members within a superclass and class.

Darren Tweedale
Greenhorn

Joined: Jan 30, 2002
Posts: 16
Hello,
To cut the short story, I have a problem understanding how clone works!
Basically I have a class (called car) that has a member (wheels). That "car" class extends another class (called transport) which the "transport" class has a member (speed).
I create a car instance and set the number of wheels to 4 and set the speed to 10. Now, if I clone the car object, it correctly sets the number of wheels but the speed isn't cloned.
This is my code - can someone please help me on how to call the clone of its superclass.

As you can see, I have sort have an answer to it, but it doesn't sound right or fair - that answer is to set each super-class member (ie speed) within the car class clone method (see XXXXX above in the code for example). Surely there must better way - besides, if I had X amount of members and X amount of class that extends "transport" class - it sounds an awful lot of work to code in! Even worse - what happens if I don't know what members to set within the superclass or when the superclass adds more members?
Any help or suggestions would be appriecated. Its doing my head in at the moment!
Thanks
Darren Tweedale
Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

Hi, on the first looks..I think you have to recheck your clone method in the car...I mean you are creating a new Car Object in the Clone...and setting only the wheels...so naturally you will get only the wheels and not the speed.
may be this article will help you understand cloning better.
Suns article on cloning
[ April 21, 2004: Message edited by: RajaniKanth Bhargava ]
Darren Tweedale
Greenhorn

Joined: Jan 30, 2002
Posts: 16
Hi, thanks for your reply RajaniKanth Bhargava,
but...
This is an example class, my real class as twenty members to set within the superclass, and 5 class extents that superclass. this means that I would have to set members of the superclass (20 times) in each class (5 times) - thats 100 lines of coding I would have to do and its repetative!
Surely a better way to do this?!
Darren Tweedale
Darren Tweedale
Greenhorn

Joined: Jan 30, 2002
Posts: 16
I think I have sort of sorted it now - not completely satisfactorily, but it will do. If anyone has any better suggestions, give me a shout!
---- within the car class

----- within the transport class
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Please help - cloaning members within a superclass and class.