Generics and returning specific subclass from method  
Author Message
sks





PostPosted: 2005-8-27 6:09:00 Top

java-programmer, Generics and returning specific subclass from method 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 ?


 
Sebastian Scheid





PostPosted: 2005-8-28 15:33:00 Top

java-programmer >> Generics and returning specific subclass from method
"sks" <email***@***.com> schrieb im Newsbeitrag
news:email***@***.com...
>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 ?


IMHO that is not possible without the risk of runtime errors. The following
gets close:
---------------------------------------------
abstract class Super<T extends Super> {

public T partialCopy() {
// may throw a ClassCastException!!
return (T) this;
}

}

class Sub1 extends Super<Sub1> {

@Override
public Sub1 partialCopy() {
return ...;
}

}
---------------------------------------------


This is not a perfect solution. Consider the following subclass of Super:
---------------------------------------------
class Sub2 extends Super<Sub1> { // !!! Sub1 instead of Sub2

}
---------------------------------------------

As long as Sub2 doesn't override partialCopy(), Super casts itself to Sub1
although it is of type Sub2.
You'd better make partialCopy abstract in Super. You lose the default
partialCopy()-method in Super but you get perfect runtime-safety due to the
absence of casting.

Regards
Sebastian


 
Ingo R. Homann





PostPosted: 2005-8-29 19:17:00 Top

java-programmer >> Generics and returning specific subclass from method Hi,

sks wrote:
> 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
> }
> }

That is of course impossible. A 'Super' is not necessarily an 'A'.

> 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 ?

Perhaps something like the following may help you:

public abstract class Sup<E extends Sup> {
public abstract E partialCopy();
}

or:

public class Sup {
int i;
public partialCopyTo(Sup s) {
s.i=i; // copy all fields
}
}

using it like this:

Sub1 s1 = new Sub1();
Sub2 s2 = new Sub2();
s1.partialCopyTo(s2);

Depends on what you really want to achieve.

Ciao,
Ingo