Data Structures and Algorithms Lecture Notes

26 January 2011 Generics


Outline

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

Remember This?

class ArrayClass {

  ArrayClass(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 Classess

Not This Way

class StringArrayClass {

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

  }

class PointArrayClass {

  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 2011 February 1.

Creative
    Commons License