Java Generic Interface Returned Generators

I found this code from the tutorial and I am assuming this method getUriBase

returns a type String

, but it is not clear which part is <T extends IEntity>

playing in this method and why is it put at the beginning?

public interface IUriMapper {
    <T extends IEntity> String getUriBase(final Class<T> clazz);
}

      

+3


source to share


2 answers


This is a generic method convention .

The syntax for a generic method includes a type parameter, inside angle brackets, and appears before the return type of the method. For static generic methods, the type parameter section must appear before the return type of the method.

If you see that before your return type String

you are defining your type parameter T

. Now coming to <T extends IEntity>

, you basically create a bounded type parameter where you tell the compiler T

to only be a subclass IEntity

.

It is important to note that before your method you need to define <T extends IEntity>

(this is what you call "generic method", more details go ...) because your interface is not a generic type, suppose your interface is like below, you don't need define a type parameter T

because the compiler needs to know what a type parameter is T

.



public interface IUriMapper<T extends IEntity> {
    String getUriBase(final Class<T> clazz);
}

      

So basically the generic method is useful (or in other words for situations) when you want to define your own type. Consider below example where-in your generic type (your interface "IUriMapper") defines a parameter of type "T", but for the method getUriBase

you create a new type "E"

public interface IUriMapper<T extends IEntity> {
    <E extends Number> String getUriBase(final Class<E> clazz);
}

      

This Oracle documentation is the best resource for learning Generics.

+3


source


Common types

The type parameter section, delimited by angle brackets (<>), follows the class name. It sets the type parameters (also called type variables) T1, T2, ... and Tn.

public class Box {
    private Object object;

    public void set(Object object) { this.object = object; }
    public Object get() { return object; }
}

      

To update the Box class to use generics, you create a generic declaration by changing the code from "public class box" to "public class box". This introduces a variable of type T that can be used anywhere within the class.

/**
 * Generic version of the Box class.
 * @param <T> the type of the value being boxed
 */
public class Box<T> {
    // T stands for "Type"
    private T t;

    public void set(T t) { this.t = t; }
    public T get() { return t; }
}

      

Source: https://docs.oracle.com/javase/tutorial/java/generics/types.html




Restricted parameters:

There may be times when you want to restrict the types that can be used as type arguments in a parameterized type. For example, a method that works with numbers can only accept instances of Number or its subclasses. For this, parameters of limited type are used.

public class Box<T> {

    private T t;          

    public void set(T t) {
        this.t = t;
    }

    public T get() {
        return t;
    }

    public <U extends Number> void inspect(U u){
        System.out.println("T: " + t.getClass().getName());
        System.out.println("U: " + u.getClass().getName());
    }

    public static void main(String[] args) {
        Box<Integer> integerBox = new Box<Integer>();
        integerBox.set(new Integer(10));
        integerBox.inspect("some text"); // error: this is still String!
    }
}

      

Source: https://docs.oracle.com/javase/tutorial/java/generics/bounded.html

0


source







All Articles