How do supertype generics in Java work? -


i've been testing supertype generics java, i've come roadblock. sample code testing:

import java.util.*;  class generictests {     public static void main( string[] args ) {               list<b> list3 = new arraylist<b>();         testmethod( list3 );     }      public static void testmethod( list<? super b> list ) {         list.add( new a() );         list.add( new b() );     } }  class { }  class b extends { } 

when compiled, error is:

generictests.java:20: error: no suitable method found add(a)             list.add( new a() );                 ^ method list.add(int,cap#1) not applicable   (actual , formal argument lists differ in length) method list.add(cap#1) not applicable   (actual argument cannot converted cap#1 method invocation conve rsion) cap#1 fresh type-variable: cap#1 extends object super: b capture of ? super b 1 error 

i thought given lower bound of b, add supertype? or apply references (i.e. arguments within method signature) because allowing supertypes break type checking?

the declaration list<? super b> list says list list of objects of type b inherits from. type need not compatible a. suppose hierarchy had been:

public class {...}  public class c extends {...}  public class b extends c {...} 

then, list list<c>. list.add(new a()) illegal. instance of a not instance of c. compiler knows instances of b or subclass can added list.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -