S
sks
I have an abstract class called say Base and two subclasses, say Sub1 and
Sub2. Let me define a method in Super which I want to return a new instance
of the superclass itself but I want to access this without having to cast.
IE,
public class Super {
public <E extends Super> E partialCopy() {
return this; // return this for example
}
}
public class Sub1 extends Super { }
Then I want to do this without the cast.
Sub1 s1 = new Sub1();
Sub2 s2 = s1.partialCopy();
Can I do this with generics ?
Sub2. Let me define a method in Super which I want to return a new instance
of the superclass itself but I want to access this without having to cast.
IE,
public class Super {
public <E extends Super> E partialCopy() {
return this; // return this for example
}
}
public class Sub1 extends Super { }
Then I want to do this without the cast.
Sub1 s1 = new Sub1();
Sub2 s2 = s1.partialCopy();
Can I do this with generics ?