Generics ClassesS2C Home « Generics Classes

With generics we also have the option to create our own generic classes which allow us to create generalized classes in much the same way as using an Object reference but with all the type safety that generics gives us.

Following is a simple generic class so you can see how we can create a generic class. We are using T as the placeholder for our formal type parameter in this instance.

We are using the getClass() method of Object to get the actual Class type which we chain to getName(), which is a method of the Class object, which can be found in the java.lang package and print this to the console. Notice how we use the T formal type parameter within the declaration, constructor and getGenericObj() getter method.


package com.server2client;

/*
  Simple generic class that accepts any object
*/
public class SimpleGenericClass<T> {

    // Generic object declaration
    private final T genericObj;

    // Pass reference to object of type T to our constructor
    public SimpleGenericClass(T genericObj) {
        this.genericObj = genericObj;
    }

    // Output object type of T to console
    public void showObjectType() {
        System.out.println("Object type of T is " + genericObj.getClass().getName());
    }
    
    // Return the generic object
    public T getGenericObj() {
        return genericObj;
    }
}

Now we need to write a test class to show how we can pass various objects to our SimpleGenericClass class:


package com.server2client;

/*
  Test our SimpleGenericClass class that accepts any object
*/
public class TestSimpleGenericClass {

    public static void main(String[] args) {

        // Test the SimpleGenericClass using a String object
        SimpleGenericClass<String> genStringObj = new SimpleGenericClass<String>("A stitch in time");
        genStringObj.showObjectType();
        String str = genStringObj.getGenericObj();
        System.out.println("String value is: " + str);
    
        // Test the SimpleGenericClass using an Integer object
        SimpleGenericClass<Integer> genIntegerObj = new SimpleGenericClass<Integer>(123);
        genIntegerObj.showObjectType();
        int i = genIntegerObj.getGenericObj();
        System.out.println("Integer value is: " + i);
    
        // Test the SimpleGenericClass using a Double primitive
        SimpleGenericClass<double> genDoubleObj = new SimpleGenericClass<double>(123.456);
        genDoubleObj.showObjectType();
        double d = genDoubleObj.getGenericObj();
        System.out.println("Double value is: " + d);
    }
}

Building the TestSimpleGenericClass class produces the following output:

Build simple generic class
Screenshot 1. Building the TestSimpleGenericClass class.

As you can see from the screenshot we got an error, as mentioned in our Generics Terminology Table you can't use primitive types with generics.

Correct the code by changing the double generic type and actual parameter type to Double for the genDoubleObj instantiation.

The amended code is shown below:


package com.server2client;

/*
  Test our amended SimpleGenericClass class that accepts any object
*/
public class TestSimpleGenericClass {

    public static void main(String[] args) {

        // Test the SimpleGenericClass using a String object
        SimpleGenericClass<String> genStringObj = new SimpleGenericClass<String>("A stitch in time");
        genStringObj.showObjectType();
        String str = genStringObj.getGenericObj();
        System.out.println("String value is: " + str);
    
        // Test the SimpleGenericClass using an Integer object
        SimpleGenericClass<Integer> genIntegerObj = new SimpleGenericClass<Integer>(123);
        genIntegerObj.showObjectType();
        int i = genIntegerObj.getGenericObj();
        System.out.println("Integer value is: " + i);
    
        // Test the SimpleGenericClass using a Double object (amended code below)
        SimpleGenericClass<Double> genDoubleObj = new SimpleGenericClass<Double>(123.456);
        genDoubleObj.showObjectType();
        double d = genDoubleObj.getGenericObj();
        System.out.println("Double value is: " + d);
    }
}

Running the amended TestSimpleGenericClass class produces the following output:

Run generic class
Screenshot 2. Running the amended TestSimpleGenericClass class.

So from the screenshot you can see that our TestSimpleGenericClass class works with the various objects passed to it replacing the T formal type parameter placeholder with the actual type parameter of the object passed.

Related Quiz

Generics Quiz 4 - Generics Classes Quiz

Lesson 4 Complete

In this lesson we looked at generics classes and how to apply them.

What's Next?

In the next lesson we look at bounded types and how to use them.