Writing Generic classes and methods Part II

Please make sure you read the part I of this article .

T is the abbrevated symbol for a type parameter, like a variable name. That means you can use any name you like for the type parameter. However, there are some conventions for naming type parameters in Java:

T for type; E for element; K for key; V for value, etc. And we should follow this convention.

The type parameter can be used as an actual type in the body of the class. As you see in the GeneralDAO class, T is used as parameter type in the save() method and as a return type in the get() method.
Type parameters are never added to the names of constructors or methods. They are used only in the names of classes and
Generic classes with more than one type parameter

public class Company<T, U> {
   T dataOne;
   U dataTwo;

   public Company(T first, U second) {
      this.dataOne = first;
      this.dataTwo = second;
   }

   public T getDataOne() {
      return dataOne;
   }

   public U getDataTwo() {
      return dataTwo;
   }
}

And the following class is generified with 3 type parameters:


public class Color<R, G, B> {
   R red;
   G green;
   B blue;

   public Color(R red, G green, B blue) {
      this.red = red;
      this.green = green;
      this.blue = blue;
   }
}

* Using bounded type parameters when writing generic classes
Suppose we want to design the GeneralADA class to work with only types that are subtypes of the Entity class (suppose Entity is the base type of all entities in the system). That means no one can declare GeneralADA<Integer> or GeneralADA<String> which we don’t expect.

Fortunately, Java generics provide the concept bounded type parameters that allow us to use the syntax <T extends X> to specify restrictions on definition of generic classes (X is a concrete type).

Here, Entity is called the upper bound, which can be any class or interface. Remember the extends keyword is used for both class and interface in the cased of bounded type parameter.
Now, with this bounded type definition, the GeneralADA class can be used only work with sub types of Entity, not with every type.
Hence the following code becomes illegal:
GeneralADA<Integer> ada = new GeneralADA<>();

ada.paySalary(new Integer());
because Integer is not a sub type of Entity.
* Using multiple bounds
We can use the syntax <T extends X & Y & Z> to define a generic class whose type parameter can be sub types of multiple types. Here, X, Y, Z can be classes and interfaces. Remember if there is a class, the class must be the first in the list.
For example, the following generic class is designed works with only types that are sub types of Awal and Fattah:

public class BadmusApp<T extends Awal & Fattah> {
   T theApp;

   public BadmusApp(T app) {
      theApp = app;
   }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: