user2102697
user2102697

Reputation: 29

Override clone in abstract superclass

=================================Before Edit========================================

I'm a bit new to Java so please bear with me :)

I've created a superclass.

public abstract class Employee

I try to override the object clone by doing the following

    @Override
    public Employee clone()
    {
        Employee foo;

        try
        {
            foo = (Employee) super.clone();
        }
        catch (CloneNotSupportedException e)
        {
            throw new AssertionError(e);
        }

        return foo;
    }

I've created a subclass

public class Employee_Subclass extends Employee

With only a constructor.

Above that I have my main program.

From the main program I'm trying to clone an object of Employee_Subclass, unsuccessfully.

Is it possible to clone an object of a subclass with only clone function in the superclass?

I keep getting the AssertionError thrown at me

    Exception in thread "main" java.lang.AssertionError: java.lang.CloneNotSupportedException: test_project.Employee_Subclass
    at test_project.Employee.clone(Employee.java:108)
    at test_project.Test_Project.main(Test_Project.java:22)
Caused by: java.lang.CloneNotSupportedException: test_project.Employee_Subclass
    at java.lang.Object.clone(Native Method)
    at test_project.Employee.clone(Employee.java:104)
    ... 1 more
Java Result: 1

Any idea how can I do that correctly?

Thanks.

==================================================================================

Ok so I added clonable, this is what I 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);
        } 

        ....
        Some Get and Set functions.
        ....

        @Override
        protected Employee clone() throws CloneNotSupportedException {
            Employee clone = (Employee) super.clone();
            return clone;
        }
   }

Here is the 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 a constructor.

And here is the main file.

public class Test_Project {

    /**
     * @param args the 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 to add throws CloneNotSupportedException otherwise it wouldn't work.

So my question is how does it exactly works?

When I call Employee_Inst.clone(), it calls the clone function in Employee, right?

Now, this function return an object of Employee, so how can I insert it into the subclass object?

And as for the deep clone, did I do it correctly? what about Date_Of_Birth_Inst, was it copied correctly?

Thanks a lot.

Upvotes: 0

Views: 935

Answers (4)

AmitG
AmitG

Reputation: 10553

You need to implement Cloneable interface. Following example might help you to understand and deep cloning and shallow cloning.

import java.util.ArrayList;
import java.util.List;

public class DeepCopy implements Cloneable {
    private List<String> hobbiesList;
    private int age;
    private String name;
    private float salary;

    public static void main(String[] args) throws CloneNotSupportedException {
        DeepCopy original = new DeepCopy();
        original.name = "AAA";
        original.age = 20;
        original.salary = 10000;
        original.hobbiesList = new ArrayList<String>();
        original.hobbiesList.add("Cricket");
        original.hobbiesList.add("Movies");
        original.hobbiesList.add("Guitar");
        original.hobbiesList.add("Eating");

        DeepCopy cloned = (DeepCopy) original.clone();
        System.out.println("original:=" + original);
        System.out.println("cloned  :=" + cloned);
        System.out.println("After adding two more hobbies in 'original' which untimately reflected in 'cloned'");
        cloned.name = "BBB";
        cloned.age = 27;
        cloned.salary = 18237;
        cloned.hobbiesList.add("Trecking");
        System.out.println("original       :=" + original);
        System.out.println("cloned  changed:=" + cloned);
    }

    @Override
    protected DeepCopy clone() throws CloneNotSupportedException {
        DeepCopy clone = (DeepCopy) super.clone();
        clone.hobbiesList = new ArrayList<String>(clone.hobbiesList);
        return clone;
    }

    @Override
    public String toString() {
        return "My name is (String)" + name + " having age (int)" + age + ". I earned (float)" + salary + " and hobbies are (ArrayList)" + hobbiesList;
    }
}

Upvotes: 0

sp00m
sp00m

Reputation: 48837

I suggest you implement a copy-constructor instead of using the Cloneable interface. See Joshua J. Bloch explanation for further details.

Upvotes: 0

Ankit
Ankit

Reputation: 6622

you need to implement Cloneable in your Employee class

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 692281

You simply forgot to make your Employee class implement Cloneable.

Upvotes: 2

Related Questions