java - Trying to sub-type classes using generics -


i having problems trying create sub-type class, , calling superconstructor arraylist of objects of generic type. trying code work. want subtypes accept list of genericobjects of type x.

    public class subtype extends supertype{          public subtype(arraylist<genericobject<y>> g) {             super(g);         }     }      public class supertype {          public arraylist<genericobject<?>> data = new arraylist<genericobject<? extends x>>();          public supertype(arraylist<genericobject<? extends x>> series) {              data.addall(series);         }     }       class genericobject<t extends x>{}     class x{};     class y extends x{}; 

i error super(g) called. says

the constructor testgenerics.supertype(arraylist>) undefined

thats kind of strange because thought generics allowed kind of thing.

if don't pass through arraylist of generic object, , pass generic object through ok.

public class subtype extends supertype{      public subtype(genericobject<y> g) {         super(g);     } }  public class supertype {      public arraylist<genericobject<?>> data = new arraylist<genericobject<? extends x>>();      public supertype(genericobject<? extends x> series) {          data.add(series);     } }  class genericobject<t extends x>{ }  class x{}; class y extends x{}; 

the above works perfectly. there way first example work?

your problem arraylist<child> not extend arraylist<parent> if child extends parent. however, on right track before, because arraylist<child> extend arraylist<? extends parent>.

you need repeat ? extends ... pattern 1 more time, this:

public supertype(arraylist<? extends genericobject<? extends x>> series) {     data.addall(series); } 

this compiles fine me.


update: full code this:

public class subtype extends supertype{      public subtype(arraylist<genericobject<y>> g) {         super(g);     } }  public class supertype {      public arraylist<genericobject<?>> data = new arraylist<genericobject<? extends x>>();      public supertype(arraylist<? extends genericobject<? extends x>> series) {          data.addall(series);     } }   class genericobject<t extends x>{} class x{}; class y extends x{}; 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -