You are on page 1of 13

Generics Part-1 || Introduction

 Case 1: Type Safety


 Array: We can choose string array, By mistake if we are trying to add any other type of object. we
will get compile time error.

 But Collections are not type safe that is we can’t give the guarantee for the type of elements
present in the collection.
 For example if our program requirement is to hold only String type of Object. If we choose AL by
mistake if we are trying to add any other type of objects we won’t get any compile time error.
 But program may fail at runtime.

 We can’t give the guarantee for the type of elements in the AL. So collections are not safe to type
caste.
 Case 2:
 Type Casting:
 In the case of Array, At the time of retrieval it is not required to perform type casting because
there is a guarantee for the type of element present in the Array.
 Arrays type casting not required, but in the case of collections at the time of retrieval compulsory
we should perform type casting because there is no guarantee for the type of elements present
inside collection.
 Need of Generics:

 By mistake if you are trying to add any other type then we will get compile time error.

 At the time of retrieval we are not required to perform type casting.


 Hence through generics we can solve type casting problem.
 Conclusion 1:
 Polymorphism concept applicable only for Base type but not for parameter type
 Usage of parent reference to hold child object is the concept of polymorphism.

 Conclusion 2:
For the type parameter we can provide any class or interface name but not primitive. If we are
trying to provide primitive then we will get Compile time error.
 AL implementation in 1.4 version:
Until 1.4 version a non-generic version of AL is declared as follows.

 The return type of get method is Object. Hence we have to perform type casting.
 A generic version of AL class is declared as above.
 Based on our requirement we can define our own generic classes also.

Part-3 || time safety || type casting Examples


 Problem with unbounded type:
 Here above program we can pass any type of type parameter to generics but the logics applicable
to only number type. So How to solve above problem?
 T: there are no restrictions hence it is unbounded type.
 Syntax for bounded type:

 Class Test<T extends X>: X can be a class or interface.


 If X is a class then as the type parameter, we can pass either X type or its child classes.
 If X is an interface then as the type parameter we can pass either X type or its implementation
class.

 We can define bounded types even in combination also.


 Class Test<T extends Number & Runnable> :
As the type parameter which should be child class of Number and implements Runnable interface.
 Note 1: we can define bounded types only by using extends Keyword and we can’t use
implements and super keywords. But we can replace implements keyword purpose with extends
keyword.

 Any variable name is valid for Type parameter

 Any number of Type parameters we can pass.

Part-4 || generics method ||generics wildcard characters(?)


 Why wild card required? To avoid duplicate code based on generic type
 m1(AL<String> l): we can call this method by passing AL of only String type.

 But within the method we can add only string type of objects to the list. By mistake if we are
trying to add other type then we will get compile time error.
 m1(AL<?> l):
1. we can call this method by passing AL of any unknown type.
2. But within the method we can’t add anything to the list except null. Because we don’t know
the type exactly.
3. null is allowed because it is valid value for any type.

 These type of methods are best suitable for read only operation but not for write operation.

 m1(AL<? extends X>): X can be either class or interface.


1.If X is a class then we can call this method by passing AL of X type or its child class.
2.If X is an interface then we can call this method by passing AL of either X type or its
implementation class.
3.But within the method we can’t add anything to the list except null because we don’t know the
type exactly.
4.These type of methods are also best suitable for read only operation.
 M1(AL<? super X>): we can call this method by passing X, X can be either class or interface.
1. If X is a class then we can call this method by passing AL of either X or its super classes
2. If X is an interface then we can call this method by passing AL of either X type or super class
of implementation class of X.

 Within the method we can add X type of Object and null to the list.

 Rules on ?:
 Right hand side ? character not allowed
Part-5 || generics method

 Communication with Non generic code:


1.If we send generic object to non-generic area then it starts behaving like non generic object.
2.Similarly if we send non generic object to generic area then it starts behaving like generic object.
3.That is the location in which object present based on that behavior will be defined.
 The main purpose of generics is to provide type safety and to resolve type casting problems.
 Type safety and type casting both are applicable at compile time. Hence generics concept also
applicable only at compile time but not run time.
 At the time of compilation as last step generics syntax will be removed and hence for the JVM
generics syntax won’t be available.
 Proof: If generics concept applicable at run time then we should get exception saying incompatable
types are inserted into the arraylist object of type string. Means at run time JVM would create an
AL object type String but it is not happening because this concept is not at run time.

 All the right-hand side are equal since the generic syntax is removed at the compile time only.
 The following declarations are equal

 For these AL objects we can add only String type objects.


 In a class two methods with same signature not allowed
 Generic Code Compilation Steps:

You might also like