java - Override clone in abstract superclass -
=================================before edit========================================
i'm bit new java please bear me :)
i've created superclass.
public abstract class employee
i try override object clone doing following
@override public employee clone() { employee foo; try { foo = (employee) super.clone(); } catch (clonenotsupportedexception e) { throw new assertionerror(e); } return foo; }
i've created subclass
public class employee_subclass extends employee
with constructor.
above have main program.
from main program i'm trying clone object of employee_subclass
, unsuccessfully.
is possible clone object of subclass clone function in superclass?
i keep getting assertionerror thrown @ me
exception in thread "main" java.lang.assertionerror: java.lang.clonenotsupportedexception: test_project.employee_subclass @ test_project.employee.clone(employee.java:108) @ test_project.test_project.main(test_project.java:22) caused by: java.lang.clonenotsupportedexception: test_project.employee_subclass @ java.lang.object.clone(native method) @ test_project.employee.clone(employee.java:104) ... 1 more java result: 1
any idea how can correctly?
thanks.
==================================================================================
ok added clonable, have
public abstract class employee implements cloneable { private string firstname; private string lastname; private string socialsecuritynumber; private date_of_birth date_of_birth_inst; // three-argument constructor public employee( string first, string last, string ssn, int day, int month, int year ) { firstname = first; lastname = last; socialsecuritynumber = ssn; date_of_birth_inst = new date_of_birth(day, month, year); } .... , set functions. .... @override protected employee clone() throws clonenotsupportedexception { employee clone = (employee) super.clone(); return clone; } }
here subclass
public class employee_subclass extends employee{ public employee_subclass( string first, string last, string ssn, int day, int month, int year ) { super( first, last, ssn, day, month, year); } }
only constructor.
and here main file.
public class test_project { /** * @param args command line arguments */ public static void main(string[] args) throws clonenotsupportedexception { employee_subclass employee_inst = new employee_subclass("hello", "world", "066499402", 7, 6, 1984); employee_subclass employee_inst1; employee_inst1 = (employee_subclass) employee_inst.clone(); } }
i had add throws clonenotsupportedexception
otherwise wouldn't work.
so question how works?
when call employee_inst.clone(), calls clone function in employee, right?
now, function return object of employee, how can insert subclass object?
and deep clone, did correctly? date_of_birth_inst, copied correctly?
thanks lot.
you forgot make employee class implement cloneable.
Comments
Post a Comment