14-11-2012, 05:38 PM
Applying Generics
Applying Generics.ppt (Size: 100.5 KB / Downloads: 24)
Generic Class or Interface
In Java, an Interface or a Class can be declared as taking one or more type parameters. If this is the case, the respective class or the interface is said to be generic.
Type Parameters are written in angle brackets. It serves a place holder for type arguments.
Each type parameter is replaced by a type argument when we create an object of the generic type.
Primary purpose of Java generics
While implementing & using Collections framework, We often require to specify that a collection contains elements of a certain type. i.e., homogenous elements
such as a list of integral values
a list of strings.
Is this feasible or desirable to create separate Implementation classes for holding homogenous collections like IntegerList, StringList, FloatList etc?
Isn’t it good to have a single implementation of the collection class and use it to hold elements of different types?
implementation of one generic class that can be instantiated for a variety of types.