yankee
yankee

Reputation: 509

Using generics to implement a common method in an abstract class

Let's say I have this hierarchy:

public abstract class AbstractEntity implements Cloneable {
    ...
    public AbstractEntity clone() {
        Cloner cloner = new Cloner();
        AbstractEntity cloned = cloner.deepClone(this);
        return cloned;
    }
}

public class EntityA extends AbstractEntity {
    ...
}

This works fine, I can do:

EntityA e1 = new EntityA();
EntityA e2 = (EntityA) e1.clone();

but I have to do a manual typecast. Is there a way to use java Generics in order to have the clone() method return the actual type of the subclass?

Thank you!

Upvotes: 6

Views: 250

Answers (2)

arcy
arcy

Reputation: 13153

I don't think generics are needed. You can declare a clone() method in the abstract class, and override it to return a subclass in the subclass.

package inheritance;

public abstract class AbstractEntity {

    public abstract AbstractEntity clone();
}


package inheritance;

public class ClassA extends AbstractEntity {

    @Override
    public ClassA clone() { return null; }

}


package inheritance;

public class Driver {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ClassA a = new ClassA();
        ClassA b = a.clone();
        System.out.println("all done");
    }

}

Isn't that what you wanted to do?

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1504121

The closest I can think of would be something like:

public abstract class AbstractEntity<T extends AbstractEntity<T>> 
    implements Cloneable {

    @SuppressWarnings("unchecked")
    public T clone() {
        Cloner cloner = new Cloner();
        T cloned = (T) cloner.deepClone(this);
        return cloned;
    }    
}

public class EntityA extends AbstractEntity<EntityA> { 
    ...
}

public class EntityB extends AbstractEntity<EntityB> { 
    ...
}

It's not entirely safe: you could write:

public class EvilClass extends AbstractEntity<EntityA> { 
    ...
}

... but whether or not that's important to you depends on your use case.

Upvotes: 6

Related Questions