Generics

CSCI 1913 – Introduction to Algorithms, Data Structures, and Program Development
Adriana Picoral

Polymorphisms in Java

  • Overloading – methods in with the same name (different parameters) in the same class
  • Overriding – methods with the same signature in different (super-sub pair) classes
  • Parametric polymorphism – Generics

“Parametric” – flexible, in this case flexibility for type

The problem to be solved

Imagine we want to write a class called Box that holds one item of any type.

public class BoxString {
    private String item;
    
    public void set(String item) {
        this.item = item;
    }
    
    public String get() {
        return item;
    }
}

The problem to be solved

Imagine we want to write a class called Box that holds one item of any type.

public class BoxPolygon {
    private Polygon item;
    
    public void set(Polygon item) {
        this.item = item;
    }
    
    public Polygon get() {
        return item;
    }
}

The problem to be solved

Imagine we want to write a class called Box that holds one item of any type.

public class BoxCard {
    private Card item;
    
    public void set(Card item) {
        this.item = item;
    }
    
    public Card get() {
        return item;
    }
}

Flawed solution

We can use the Object class as type:

public class Box {
    private Object item;
    
    public void set(Object item) {
        this.item = item;
    }
    
    public Object get() {
        return item;
    }
}

Flawed solution

Box box = new Box();
box.set("Hello");
String str = (String) box.get();  // need to cast - annoying!

box.set(42);
String oops = (String) box.get();  // compiles fine, crashes at runtime!
  • Type casting every time we retrieve something
  • Compiler can’t catch type errors - code crashes at runtime
  • No type safety

Generics

  • Enables code reuse and flexibility
  • Code works independently of the types of values it operates on

Generics allow us to write code that works with multiple types while maintaining type safety.

  • Parametrically polymorphic methods are called generic methods
  • Parametrically polymorphic data types are called generic data types

Box with generics

public class Box<T> {
    private T item;
    
    public void set(T item) {
        this.item = item;
    }
    
    public T get() {
        return item;
    }
}

Box with generics

Box<String> stringBox = new Box<>();
stringBox.set("Hello");
String str = stringBox.get();  // no cast needed!

Box<Integer> intBox = new Box<>();
intBox.set(42);
// intBox.set("Hello");  // compiler error - caught at compile time!