Generic Class Hierarchies

In this chapter you will learn:

  1. How to extend generic class
  2. How to be a generic subclass
  3. Run-Time Type Comparisons Within a Generic Hierarchy
  4. How to override methods in a generic class

extending Generic Class

A generic class can act as a superclass or be a subclass. In a generic hierarchy, any type arguments needed by a generic superclass must be passed up the hierarchy by all subclasses.

Using a Generic Superclass

class MyClass<T> {
  T ob;//  j a va 2 s.c o  m
  MyClass(T o) {
    ob = o;
  }
  T getob() {
    return ob;
  }
}
class MySubclass<T, V> extends MyClass<T> {
  V ob2;

  MySubclass(T o, V o2) {
    super(o);
    ob2 = o2;
  }

  V getob2() {
    return ob2;
  }
}
public class Main {
  public static void main(String args[]) {
    MySubclass<String, Integer> x = new MySubclass<String, Integer>("Value is: ", 99);
    System.out.print(x.getob());
    System.out.println(x.getob2());
  }
}

A Generic Subclass

It is perfectly acceptable for a non-generic class to be the superclass of a generic subclass.

class MyClass {//  ja v  a2 s  .  c o  m
  int num;

  MyClass(int i) {
    num = i;
  }

  int getnum() {
    return num;
  }
}
class MySubclass<T> extends MyClass {
  T ob;
  MySubclass(T o, int i) {
    super(i);
    ob = o;
  }
  T getob() {
    return ob;
  }
}
public class Main {
  public static void main(String args[]) {
     MySubclass<String> w = new MySubclass<String>("Hello", 4);
    System.out.print(w.getob() + " ");
    System.out.println(w.getnum());
  }
}

Run-Time Type Comparisons Within a Generic Hierarchy

The instanceof operator can be applied to objects of generic classes.

class Gen<T> {
  T ob;/*from j  a  v  a2s.c o  m*/

  Gen(T o) {
    ob = o;
  }
  T getob() {
    return ob;
  }
}
class Gen2<T> extends Gen<T> {
  Gen2(T o) {
    super(o);
  }
}

public class Main {
  public static void main(String args[]) {
    Gen<Integer> iOb = new Gen<Integer>(88);
    Gen2<Integer> iOb2 = new Gen2<Integer>(99);
    Gen2<String> strOb2 = new Gen2<String>("Generics Test");
    System.out.println("iOb2 is instance of Gen2"+(iOb2 instanceof Gen2<?>));
    System.out.println("iOb2 is instance of Gen"+(iOb2 instanceof Gen<?>));
    System.out.println("strOb2 is instance of Gen2"+(strOb2 instanceof Gen2<?>));
    System.out.println("strOb2 is instance of Gen"+(strOb2 instanceof Gen<?>));
    System.out.println("iOb is instance of Gen2"+(iOb instanceof Gen2<?>));
    System.out.println("iOb is instance of Gen"+(iOb instanceof Gen<?>));
  }
}

The output:

Overriding Methods in a Generic Class

A method in a generic class can be overridden like any other method.

class Gen<T> {
  T obj;/*from  j av a2 s  .  co m*/
  Gen(T o) {
    obj = o;
  }
  T getob() {
    System.out.print("Gen's getob(): ");
    return obj;
  }
}
class Gen2<T> extends Gen<T> {
  Gen2(T o) {
    super(o);
  }
  T getob() {
    System.out.print("Gen2's getob(): ");
    return obj;
  }
}
public class Main {
  public static void main(String args[]) {
    Gen<Integer> iOb = new Gen<Integer>(88);
    Gen2<String> strOb2 = new Gen2<String>("Generics Test");
    System.out.println(iOb.getob());
    System.out.println(strOb2.getob());
  }
}

Next chapter...

What you will learn in the next chapter:

  1. How to cast generic types
Home » Java Tutorial » Generics
Generic Type
Generic Bounded Types
Generic Method
Generic Constructors
Wildcard
Generic interface
Raw Types and Legacy Code
Generic Class Hierarchies
Cast generic types
Generic Restrictions