My class implements a superclass method that returns a List<JComponent> . The returned list is read-only:
public abstract class SuperClass { public abstract List<JComponent> getComponents(); }
In my class, I want to return a field that is declared as List - i.e. sub-list:
public class SubClass extends SuperClass { private List<JButton> buttons; public List<JComponent> getComponents() { return buttons; } }
This generates a compiler error because List<JButton> not a subtype of List<JComponent> .
I can understand why it does not compile, since you should not add a JTextField to the JButtons list.
However, since the list is read-only, then "conceptually" this should be allowed. But, of course, the compiler does not know that it is read-only.
Is there a way to achieve what I want to achieve without changing the declaration of the method in the superclass and the declaration of the field in the subclass?
Thanks Calum
java generics
Calum
source share