• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please help - cloaning members within a superclass and class.

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 128
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic