Session 6: Java Generics


Java Generics

Q? How will you manage if you want the same behaviour for different kind of classes
/> in this particular case we use the Object References to accommodate any object type
/> it is possible only through using Generic Classes and Methods

ex: Generic Class
public class Product<T> {
String name;
String ingredient;
T ID;
Product (String n,String i, T ID){
this.name = n;
this.ingredient = i;
this.ID = ID;
}
T getID(){
return ID;
}
}

Q? How generic type declaration is done
/> Done in this way :
public (class|interface) NameOfClass <P1 {,P2}>

P1 and P2 can be usually : T(type) , E(element), K(key), V(value)


Q? What about using generics in Collection framework
/> All the collection interfaces and classes have been redefined as Generics so it can be easily used.
/> while using generics : the declaration is longer but the use is more safe.
Student<Integer> a = new Student<Integer> (“A1”,”A”, new Integer(123)); // with generics
Student a = new Student (“A1”,”A”, new Integer(123)); // without generics
Integer matricola1 = a.getID( );

Q? what are the advantages of using Generics
/> Using Generics in java programming leads to safer programming , more compact , easier to understand and leads to high performance.

Q? what is Diamond Operator
/> The class parameter and the reference parameter must be same.
Map<String, Object> name = TreeMap<String,Object>( );
Map<String, Object> name = TreeMap<>( ); // also this can be used


Q? what are Bounded types
/> we can also express the contraints when defining generic types and this types are called bounded types
class C <T extends B1 { & B2}>
=> means class C can only be defined with types derived from B1(and B2 )
class C <T super D>
=> means class C can only be defined with types that are super class of D (including D also )



Q? how to handle generic types while using inheritance
/> String is a subtype of Object : in case of generic type we cannout use directly this:
List<String> is not subtype of List<Object> so we cannot use inheritance here

Note *: Generic type are Invariant(never changing) so we always try to subsitute a more general (super class) reference to a specific one
Note *: In case of Array sometimes the run-time type clash are possible

Q? what is this wildcard
/> Is usually used when we have no contraints when using generic types.
Ex:In case of collection the generic type sometimes fails to work
so we use wildCard:
Collection <?> c ; i.e. collection of unknown, unbounded
Collection <? extends B> : upper bound : only sub-type of B(including B)
Collection <? super D > : lower bound : only super-type of D(including D)


Q? how generic method is declared
/> following format
modifiers <T> ret_type name (parameters)
<T> T method (T t)
<N extends Number> void method( List<N> t)
void method (List<? Extends Number> t)
*modifiers = (public,private,protected,static,final, abstract)

Q? what are type erasures
/> To implement generics, the Java compiler applies type erasure.
What is done? it replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, thus, contains only ordinary classes, interfaces, and methods.
It also inserts type casts if necessary to preserve type safety.
Generates a bridge methods to preserve polymorphism in extended generic types:
some of the consequences are :
/> compiler takes control only when the generic type is used, not beyound that
/> not possible to instantiate an object of the generic's parameter type
/> not possible to implement two generic interfaces instantiated with different types.

Comments

Popular Posts