Reputation: 9541
Is it possible to make a class extends from a generic type?
I want to subclass different components, either with implements
or extends
to add uniform functionality to all the subclasses.
What I'm trying to achieve is this:
class MyTable extends MyAbstractComponent<JTable> {...}
class MyPanel extends MyAbstractComponent<JPanel> {...}
MyAbstractComponent t = new MyTable();
MyAbstractComponent p = new MyPanel();
container.add(t);
container.add(p);
In this case, how would I formulate MyAbstractComponent
?
I tried the following, but it gives me the error "Found type: parameter C. Expected: class":
abstract class MyAbstractComponent<C extends Component> extends C {...}
Upvotes: 2
Views: 1298
Reputation: 44071
You have already got a good answer from the compiler, so it is not possible to let a class extend a type parameter. A type parameter is not a class. Instead of abstract class MyAbstractComponent<C extends Component> extends C {...}
I would simply leave out the generics (not so useful here) or write:
abstract class MyAbstractComponent<C extends Component> extends Component {...}
Remark about the wish for overriding getSelectedRows()
:
This could only be possible in a JTable subclass, but not in your panel subclass. In this case I recommend another approach:
Redefine your subclasses.
class MyTable extends JTable implements MyAbstractComponent // here override getSelectedRows() class MyPanel extends JPanel implements MyAbstractComponent
If you already have some method implementations in your MyAbstractComponent then consider to move this code in a helper class which would be called by MyTable and MyPanel. Generics are not really so useful in an environment (SWING) which does not use this language feature due to historic reasons.
Anyway, if you are interested in learning more about generics I recommend Angelika Langers tutorial and faqs about generics (just google).
Upvotes: 5
Reputation: 4940
I did sth. similar for my DAO layer
public interface EntityDAO<E, I> {
public boolean update(final E entity, Connection connection);
public E getByID(I id, Connection connection);
public boolean delete(final I identifier, Connection connection);
}
public abstract class AbstractEntityDAO<E extends Entity, I extends Number> implements EntityDAO<E, I> {
@Override
public boolean update(E entity, Connection connection) {
// TODO Auto-generated method stub
return false;
}
@Override
public E getByID(I id, Connection connection) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean delete(I identifier, Connection connection) {
// TODO Auto-generated method stub
return false;
}
}
And finally the concrete subclass will define the types of the entity object.
public class AddressEntityDAO extends AbstractEntity<Address, Integer> {
@Override
public boolean update(Address entity, Connection connection) {
return super.update(entity, connection);
}
@Override
public Address getByID(Integer id, Connection connection) {
return super.getByID(id, connection);
}
@Override
public boolean delete(Integer identifier, Connection connection) {
return super.delete(identifier, connection);
}
}
Upvotes: 0