Implementing the Raw Type for the Generic Interface
Every generic interface has a raw type. When you implement an generic interface in an ordinary class without specifying a type argument, your class is implementing the raw type, so the methods in the interface are declared with parameters and/or return types of type Object. For example :
The MyClass implements the raw type for the MyInterface by without specifying a type argument. So, the method showType() implements with the parameter of type Object. The method showType( ) accept any type of argument. Suppose you have specified that the type parameter T for a parameterized type is bounded by the type MyInterface<T>. For example :
In the raw type for the MyClass, all occurrences of the type variable T are replaced by MyInterface. The raw type corresponding to MyInterface<T> is produced by using type Object as the replacement for the type parameter because no parameter constraints are specified for the MyInterface<T> type. Thus for the MyClass<T> type, the raw type definition is produced by substituting MyInterface in place of the type variable, T. This may be what you want for a valid raw type in this case. The parameter type to the setT( ) method is MyInterface, so you can pass an object of any class type that implements the MyInterface interface to it. For example :
However, in other instances, you may want the raw type to be produced using Object as the upper bound for the type parameter. You can accomplish this quite easily by simply defining the first bound for the type parameter as type Object. For example :
Now the leftmost bound for the type parameter is type Object, so the raw type is produced by replacing the type variable T by Object in the generic type definition. For example :
Now the myobj.getT( ) method returns a reference of type Object instead of type MyInterface.
Program
Program Source
interface MyInterface<T> { void showType(T t); } class MyClass implements MyInterface { public void showType(Object t){ System.out.println("t -> "+t.getClass().getName()); } } public class Javaapp { public static void main(String[] args) { MyClass mobj = new MyClass(); mobj.showType(50); mobj.showType("Fifty"); } }