Tutorials :Flexible Object Operation with Java Generics :

Handling Exceptions

To start, you should know that exceptions themselves cannot be generic. Beyond that, you also have to know that the throws clause (not the catch clause) accepts type parameters. Here's a simple example:
public <T extends Throwable> void foo(T exception) throws T
{ throw exception; }

Generic Classes

In this section, you'll see how to create a generic class and call its methods. Listing 6 shows a POJO class, which takes advantage of Java Generics.

Now take a look at three examples of using this class:

  • Example 1:
       GenericClass<String,String,String> strings = 
    new GenericClass<String,String,String>("MyT","MyS","MyQ");
       String myT = strings.getT();
       String myS = strings.getS();
       String myQ = strings.getQ();
       System.out.println("MyT="+myT+" MyS="+myS+" MyQ="+myQ);
       strings.setT("YourT");
       String yourT = strings.getT();
       System.out.println("YourT="+yourT);
    
    Output:
    MyT=MyT MyS=MyS MyQ=MyQ
    YourT=YourT
    
  • Example 2:
       GenericClass<Integer,Integer,Integer> ints = 
    new GenericClass<Integer,Integer,Integer>(1,2,3);
       Integer my1 = ints.getT();
       Integer my2 = ints.getS();
       Integer my3 = ints.getQ();
       System.out.println("My1="+my1+" My2="+my2+" My3="+my3);
       ints.setT(4);
       Integer your4 = ints.getT();
       System.out.println("Your4="+your4);
    
    Output:
    My1=1 My2=2 My3=3
    Your4=4
    
  • Example 3:
       GenericClass<String,Integer,Float> mixt= 
    new GenericClass<String,Integer,Float>("MyTmixt",1,10.0f);
       String myTmixt = mixt.getT();
       Integer my1mixt = mixt.getS();
       Float myfmixt = mixt.getQ();
       System.out.println("MyTmixt="+myTmixt+" My1mixt="+my1mixt+" Myfmixt="+myfmixt);  
    
    Output:
    MyTmixt=MyTmixt My1mixt=1 Myfmixt=10.0
    
When you work with generic classes, it's good to keep in mind the following:
  • A generic class is shared by all its instances. This means that all instances of a generic class belong, at runtime, to the same class—independently of their type parameters (static methods and variables are also shared by all instances). Testing this theory on the above GenericClass class can be done like this:
        System.out.println(strings.getClass()==ints.getClass());      
    //it will print “true”
    
  • Don’t use instanceof and casts. As a consequence of the above point, it's not recommended to use explicit casts together with the instanceof operator for generic classes (this rule also applies to type variables). Test this theory on the above GenericClass class like this:
    if(strings instanceof GenericClass)
          { System.out.println("TRUE"); }
            else { System.out.println("FALSE"); } 
    //it will not compile
    
  • java.lang.Class is a generic class. Starting with Java 5, this class has a type parameter (T), that is used for the type that the Class object is representing. For example, the Float.class is Class<Float>, the String.class is Class<String>, the Integer.class is Class<Integer>, and so on.
  • Classes can extend Generic Classes.. The extension mechanism is applicable to generic classes, as you can see from examples below:
    //example 1
    class MyStrings extends LinkedList<String>{}
    
    //example 2
    class GenericClassX<N,M> extends GenericClass<String, N, String>{}
    
  • Method overriding rules has changed. Normally, when you override a method, its signature should be the same as the signature of the method that you override. But Java Generics changes this rule. For example, suppose you have the following:
    class MyStrings extends LinkedList<String>{}
    
    Then the prototypes of the remove method are:
    public Object remove(int index) – for LinkedList class
    public String remove(int index) – for MyStrings class
    
  • Inheritance changes the return type. Since Java Generics is making the rules, the return type of an overridden method can be different from the return type of the method it overrides. Actually, the new rule sounds like this: The return type of the method must be a subtype of all the methods it overrides. Listing 7 shows two examples: one where this rule is applied incorrectly and one correct application.

Home / Articles / Flexible Object Operation with Java Generics / 1 / 2 / 3 / 4 /Next: Generic Methods

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.