reverse_engineer
reverse_engineer

Reputation: 4269

Extending ArrayList of classes implementing certain classes containing generics doesn't work?

Someone knows why this doesn't work?

public class ArrayList<E extends EXTDBinterface<T>> 
    extends java.util.ArrayList<E> implements List<E>,
    RandomAccess, Cloneable, java.io.Serializable {}

Eclipse complains about T not being resolved to a type... Is it impossible to extend the ArrayList class for types extending EXTDBinterface<T>? The point is I would like to use T inside the class, but sadly only this works:

public class ArrayList<E extends EXTDBinterface> 
    extends java.util.ArrayList<E> implements List<E>,
    RandomAccess, Cloneable, java.io.Serializable {}

but then how to get the generic type T of EXTDBinterface at runtime...?

Upvotes: 0

Views: 627

Answers (3)

bellum
bellum

Reputation: 3710

First doesn't work cause you didn't set type T. Also you don't need to extend and implement all stuff again cause ArrayList has had this yet. Write, for example:

class ArrayList<T,E extends EXTDBinterface<T>> 
extends java.util.ArrayList<E>{}

Upvotes: 3

PermGenError
PermGenError

Reputation: 46408

You have to define T as the type argument for the class

public class ArrayList<T, E extends EXTDBinterface<T>> 
    extends java.util.ArrayList<E> implements List<E>,
    RandomAccess, Cloneable, java.io.Serializable {}

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272237

how to get the generic type T of EXTDBinterface at runtime

You can't do that given that Java makes use of type erasure. If you need the actual type, then you'll have to pass that in (normally as a Class-typed parameter)

Upvotes: 0

Related Questions