Raw Types and Legacy Code

In this chapter you will learn:

  1. How to use generic class in a non-generic way

Deal with legacy code

To handle the transition to generics, Java allows a generic class to be used without any type arguments.

Here is an example that shows a raw type in action:

class MyClass<T> {
  T ob;/*from  j  av  a  2s .  co m*/
  MyClass(T o) {
    ob = o;
  }
  T getob() {
    return ob;
  }
}
public class Main {
  public static void main(String args[]) {
    MyClass raw = new MyClass(new Double(98.6));
    double d = (Double) raw.getob();
    System.out.println("value: " + d);
  }
}

Output:

Next chapter...

What you will learn in the next chapter:

  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
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