clone - How to implement a deep copy of an object that contains an array of objects in Java? -


i need make class called garage implement cloneable , override object.clone() method make deep copy of object of type garage contains array of objects of type vehicle.

i have read on several answers cloneable broken , wise not use it, lab assignment demands implement cloneable , code proper clone() method. have been looking long still haven't been able this. appreciated.

this code garage class:

public class garage extends vehicle implements cloneable {     // array store 15 vehicle objects     vehicle array[] = new vehicle[15];      // overriding clone method perform deeper copy     @override     protected object clone() throws clonenotsupportedexception {         //todo     } } 

this code vehicle class:

public class vehicle {     int numwheels;     int id;     string name; } 

first of garage extends vehicle makes no sense. i'm assuming it's mistake. garage not vehicle.

in garage can implement clone :

@override protected object clone() {     garage other = new garage ();     (vehicle v : this.array) {         other.addvehicle((vehicle)v.clone());     }     return other; } 

then have implement clone() in vehicle class hierarchy.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -