Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cloning 1

Status
Not open for further replies.

mj616

MIS
Jun 23, 2003
73
US
I have a question about cloning. If I have a super class called MotorVehicle who has :
private Engine engine= new Engine();

I also have a subClass called Car. I want to be able to clone the Car subclass and also the engine, is there a way to do this?

Ive tried the following but cant do it because Engine is declared private in the MotorVehicle.


public Object clone()
{
try{
Car cloned= (Car)super.clone();
cloned.engine=(Engine)engine.clone();
}
catch(CloneNotSupportedException e){
return null
}

}

Of course I also have a constructor which does super(engine). Is there a way I can clone the engine without being in MotorVehicle?

Thanks to anyone who responds.








 
It appears that MotorVehicle does not have a properly formed clone() method. i.e. it doen't return a useable clone, as it hasn't created an engine.

In this case you can either correctly define the clone method in MotorVehicle (if you can!), or instantiate a second Car, and make it have the same properties as the Car your cloning, then return it.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top