Data Structures & Algorithms Lecture Notes

17 September 2010 • Generics


Outline

  • A problem with types.
  • Generics
    • Basics
    • Implementation via erasure.
    • Properties
  • Interfaces
  • Polymorphism
generic can

Remember This?

class ArrayADT {

  ArrayADT(int n) { ... }
  void resize(int n) { ... }

  int get(int i) { ... }
  int size() { ... }

  void put(int i, int v) { ... }
  void swap(int i, int j) { ... }

  // and so on.
  }

Monotyped ADTs

Not This Way

class StringArrayADT {

  String get(int i) { ... }
  void put(int i, String v) { ... }

  }

class PointArrayADT {

  Point get(int i) { ... }
  void put(int i, Point v) { ... }

  }

Type Specialization

Wishful Thinking

Type-Variable Example

Generics

Generic Type Variables

Generic Class Names

Using Type Variables

Type Variables vs new

Type Variables vs static

Implementing Generics

Erasure

Erasure Example.

Erasure Example..

Erasure Example...

Erasure Example....

Observations

Repercussions

Type Variables vs new

Interfaces

Generic Interfaces

Implementing Generic Interfaces

Polymorphism

Summary

References

Credits


This page last modified on 28 January 2010.

Creative
    Commons License