ABHAY KUMAR
ABHAY KUMAR

Reputation: 11

Cloning super class with abstract clone() method

The problem in the code is that we cannot directly invoke the abstract method clone for the type Item and i don't want to modify the Item class. Is there any way, or I should change my logic?

 abstract class Item implements Cloneable {

    private boolean stackable; 
    protected String name; 

    public Item()
    {
        this.name      = new String( "Air" );
        this.stackable = true;
    }
    public Item( String name )
    {
        this.name      = name;
        this.stackable = true;
    }
    public abstract Item clone();
}




class Tool extends Item {

    protected double durability; 

    public Tool()
    {
        super("", false);
        this.durability    = 0;          
    }
    public Tool(Tool src)
    {   
        this.durability    = src.durability;              
    }

    public Item clone() throws CloneNotSupportedException {

         Object obj = super.clone();   //problem is here

        return (Item) obj;
   }
}

Upvotes: 1

Views: 306

Answers (4)

SirRollalot
SirRollalot

Reputation: 41

You can use reflection to identify if there is a clone-method in the object. May you can also tag your classes with the clonable interface to make it clear that there is a method so you can check for that first with the instanceof operator cuz i think that reflection is performance relevant as far as i know.

for me it looks like a general design thing of java, i also expected that the cloneable-interface should contain a signature to the clonemethod itself, but thats not my cup of tee.

hope this was helpful, wish you a nce day

Upvotes: 0

Sandeep Pandey
Sandeep Pandey

Reputation: 1132

public class Example {
    public static void main(String[] args) throws CloneNotSupportedException {
        Tool t= new Tool();
        System.out.println(t.hashCode());

        Tool t1=(Tool) t.clone();
        System.out.println(t1.hashCode());
    }

}

abstract class Item{

    private boolean stackable;
    protected String name;

    public Item()
    {
        this.name      = new String( "Air" );
        this.stackable = true;
    }
    public Item( String name )
    {
        this.name      = name;
        this.stackable = true;
    }
}




class Tool extends Item implements Cloneable {

    protected double durability;

    public Tool()
    {
        super();
        this.durability    = 0;
    }
    public Tool(Tool src)
    {
        this.durability    = src.durability;
    }

    public Item clone() throws CloneNotSupportedException {

        Object obj = super.clone();   //problem is here

        return (Item) obj;
    }
}

Upvotes: 0

Raguel
Raguel

Reputation: 625

Somehow i removed your error

EDIT

abstract class Item implements Cloneable{

    private boolean stackable;
    protected String name;

    public Item()
    {
         this.name      = new String( "Air" );
         this.stackable = true;
    }
    public Item( String name, boolean check ) {
        this.name      = name;
        this.stackable = check;
    }

     @Override
     public abstract Item clone() throws CloneNotSupportedException;
}




class Tool extends Item  {

    protected double durability;

    public Tool()
    {
         super("", false);
         this.durability    = 0;
    }
    public Tool(Tool src)
    {
         this.durability    = src.durability;
    }

    @Override
    public Item clone() throws CloneNotSupportedException {

         Item obj = (Item) this.clone();

         return (Item) obj;
    }
}

Upvotes: 1

Sandeep Pandey
Sandeep Pandey

Reputation: 1132

implement Tool class with Cloneable interface and override clone()

Upvotes: 0

Related Questions