Forum Stats

  • 3,750,513 Users
  • 2,250,187 Discussions
  • 7,866,997 Comments

Discussions

Generics and Inheritance

user5876281
user5876281 Member Posts: 2
edited Oct 2, 2013 10:06PM in Generics

Hello,

I am new to generics and trying to change our existing code to use generics.

We have one abstract super class and multiple sub classes. Super class contains a collection that should contain values only of a specific type bases on the sub class. Question I have is, how do I tell super class what type to use for that collection?

Here is the sample code.

Super Class:


import java.util.ArrayList;
import java.util.List;

public class BusinessModelObjectCollection <T>{
    private List <T> bmoColl;
   
    public <T> BusinessModelObjectCollection(){
        bmoColl = new ArrayList<>();
    }
}

Sub Class:

public class AssignableWorkTypeCollection extends BusinessModelObjectCollection {
   
    public AssignableWorkTypeCollection(){
        super<AssignableWorkTypeCollection>();
    }
}

I am getting compile time error in subclass when I try to pass the type when I call the constructor.

I will really appreciate your help on this.

Thanks,

Swetal

Best Answer

Answers

This discussion has been closed.