简体   繁体   中英

Generic class to derive from its generic type parameter

Compiles:

public class SerializableObject<T> implements Serializable
{
 public T m_object;
}

Does NOT compile:

public class SerializableObject<T> extends T implements Serializable
{
}

So, I want a generic class to derive from its generic type parameter.
Why?
Let's say I have a Map<K, V> and I simply want to serialize it.
I also don't know ahead which keys I'll have.

How do I do that?

So, I want a generic class to derive from its generic type parameter.

You just can't do that, I'm afraid. There are various technical reasons for this, not least of which is type erasure.

You should look for an alternative solution to your issues - this idea is a dead end.

Nope, your goal is near a Dynamic type, which java does not support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM