Java generics - overriding an abstract method and having return type of the subclass?

This will work: public abstract class SuperClass{ public abstract SuperClass getSelf(); } public class SubClass extends SuperClass{ @Override public SubClass getSelf(){ return this; } } Notice I've added extends SuperClass to your SubClass definition. The return type of getSelf is referred to as a covariant return type.

Ahh, of course I forgot to add extends SuperClass in the example though that certainly exists in my code... That said, I was totally unaware that this already worked, thanks! – Numeron Jun 23 at 7:41.

How about this: public abstract class SuperClass> { public abstract T getSelf(); } public class SubClass extends SuperClass { public SubClass getSelf() { return this; } } I know it's quite repetitive and nothing bounds the type to be the same SubClass instance, because also AnotherSubClass would satisfy the bound, but at least it should do the trick.

Here is how to do it (Since JDK 1.5 there is this thing called covariant return types, where something like this is possible). Abstract class SuperClass>{ public abstract T getSelf(); } class SubClass extends SuperClass { public SubClass getSelf() { return this; } } public class Generics { public static void main(String args) { System.out. Println(new SubClass().getSelf()); } } Notice a similar class generic definition with Enum (http://download.oracle.com/javase/1,5.0/docs/api/java/lang/Enum.html) See what happens behind the scenes (by using javap SuperClass SubClass): class SubClass extends SuperClass{ SubClass(); public SubClass getSelf(); public SuperClass getSelf(); } abstract class SuperClass extends java.lang.

Object{ SuperClass(); public abstract SuperClass getSelf(); } Notice how the subclass method has a different return type, which is a subtype of the super method return type. Btw, notice that public SuperClass getSelf(); in class SubClass is actually a synthetic method.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions