Posted in Core Java, Object Oriented Programming

Generics

A class, interface or method that declares one or more type variables (type parameters).

Advantages:

  • Type-Casting
  • Type-Safety (single type of objects)
  • compile-time checking

public Class Box{                                     ——–>              public Class Box<T>{

private Object obj;                                                                              private T t;

public void set (Object obj){                                                             public void set(T t){

this.obj=obj;                                                                                          this.t=t;

}                                                                                                        }

public Object get(){                                                                               public T get(){

return obj;                                                                                              return t;

}                                                                                                           }

}                                                                                                          }

Leave a comment