Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package generics;
- import java.util.ArrayList;
- public class Generics_Main {
- public static void main(String[] args) {
- // Base is abstract
- // Derivative1 extends Base
- // Derivative2 extends Base
- ArrayList<Derivative1> derivative1s = new ArrayList<>();
- ArrayList<Derivative2> derivative2s = new ArrayList<>();
- ArrayList<String> strings = new ArrayList<>();
- foo( derivative1s );
- foo( derivative2s );
- foo( strings ); // not working. if u want it to work, replace the
- // "<K extends Base>" with "<K>"
- }
- public static <K extends Base> void foo(ArrayList<K> list) {
- // do something
- for (K k : list) {
- // do something
- }
- // do something
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement