I have two formats of a class BaseButton:
public abstract class BaseButton<E extends BaseButton<E>> extends JXButton implements Initializer {
private static final long serialVersionUID = 11L;
public BaseButton() {
LibraryLogger.initMessage(ClassUtils.getInstance().getResolvedClassName(this.getClass()));
initialize();
}
public abstract void initialize();
}
And:
public abstract class BaseButton<E> extends JXButton implements Initializer {
private static final long serialVersionUID = 11L;
public BaseButton() {
LibraryLogger.initMessage(ClassUtils.getInstance().getResolvedClassName(this.getClass()));
initialize();
}
public abstract void initialize();
}
And I am creating child classes as:
public class GreenButton extends BaseButton<GreenButton> {
}
My question is what is the difference between these two types of generic notation?
Which one is better and why?
Any information will be very helpful to me.
Thanks.
With the code you provided, it makes no difference.
However, when you start using the generic type
E, that’s when you’ll start seeing a difference.In the first example,
Ewill have all the methods and members ofBaseButton. So, ifBaseButtonhas a methodint foo(String s), you will be able to say:If you try the same thing with the second example, the compiler will report an error, because
Eimplicitly extendsObjectandObjectdoes not have afoomethod.