Tag: generics

Java Generics extends

If you pass Long and/or Integer to a method then signature of the method is void m(Number n)
If you pass List and/or List to a method then signature of the method is void m(List<? extends Number> numbers) otherwise compilation error of incompatible types.

package com.bawi;

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

public class TestExtendsGenerics {

    public static void main(String[] args) {
        int i = 1;
        List<Integer> ints = new ArrayList<>();
        ints.add(i);

        long l = 1L;
        List<Long> longs = new ArrayList<>();
        longs.add(l);

        printNumber(i);

        // Error: incompatible types: 
        // java.util.List<java.lang.Integer> cannot be converted to java.util.List<java.lang.Number>
        //printNumberList(ints);
        
        printExtendsNumberList(ints);


        printNumber(l);

        // Error: incompatible types: 
        // java.util.List<java.lang.Long> cannot be converted to java.util.List<java.lang.Number>
        //printNumberList(longs);
        
        printExtendsNumberList(longs);
    }
    private static void printNumber(Number number) {
        System.out.println(number);
    }

    private static void printNumberList(List<Number> numbers) {
        System.out.println(numbers);
    }

    private static void printExtendsNumberList(List<? extends Number> numbers) {
        System.out.println(numbers);
    }
}

Generics with java collection

import java.util.Arrays;
import java.util.List;

public class MyGenerics {
    static class SuperClass {}
    static class SubClass extends SuperClass {}

    public static void main(String[] args) {
// get1:
        List<SuperClass> superClasses = get1(Arrays.asList(new SuperClass()));

        // Error: java: incompatible types: List<MyGenerics.SuperClass> cannot be converted to List<MyGenerics.SubClass>
        //List<SubClass> subClasses = get1(Arrays.asList(new SubClass()));

// get2 (none compiles):
        // Error: java: incompatible types: List<capture#1 of ? extends MyGenerics.SuperClass> cannot be converted to List<MyGenerics.SuperClass>
        //List<SuperClass> superClasses2 = get2(Arrays.asList(new SuperClass()));

        // Error: java: incompatible types: List<capture#1 of ? extends MyGenerics.SuperClass> cannot be converted to List<MyGenerics.SubClass>
        //List<SubClass> subClasses2 = get2(Arrays.asList(new SubClass()));

// get3: (all compiles):
        List<SuperClass> superClasses3 = get3(Arrays.asList(new SuperClass()));
        List<SubClass> subClasses3 = get3(Arrays.asList(new SubClass()));
        List<SuperClass> subClasses3_1 = get3(Arrays.asList(new SubClass()));
        List<SuperClass> subClasses3_2 = get3(Arrays.asList(new SubClass(), new SuperClass()));
    }

    static List<SuperClass> get1(List<SuperClass> input) { return null; }

    static List<? extends SuperClass> get2(List<? extends SuperClass> input) { return null; }

    static <T extends SuperClass> List<T> get3(List<T> input) { return null; }
}