Generics and returning specific subclass from method

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

Sebastian Scheid

sks said:
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
 
I

Ingo R. Homann

Hi,
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

generics puzzle 57
can this be done with generics? 32
Generics 12
Generics and Polymorphism 5
Generics 24
Subclass of subclass 0
Generics annoyance 18
Generics: factory method for returning parameterized instance 3

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top