Types and Polymorphism: CS 305 & 503 Lecture notes

Data Structures & Algorithms Lecture Notes

10 September 2009 • Types and Polymorphism


Outline

Types

Strong Typing

Type Strictness

Polymorphism

Java Types

Reference Types

Primitives vs. References

Primitives as References

References as Primitives

Automatic Conversions

Boxing Examples

void m() {
  Object o = 42;
  }

void m(String key) {
  if (hashTable.containsKey(key))
    int i = hashTable(key);
  }

Java Types

Class Types

The IS-A Relation

class Dictionary 

  void add(Key k, Value v) { ... }
  Value find(Key k) { ... }
  void delete(Key k) { ... }

class LinkedListDictionary 

  void add(Key k, Value v) { ... }
  Value find(Key k) { ... }
  void delete(Key k) { ... }

class HashTableListDictionary 

  void add(Key k, Value v) { ... }
  Value find(Key k) { ... }
  void delete(Key k) { ... }

IS-A Abstraction

Inheritance

The Inheritance Relation

Why Inheritance?

Class Compatibility

Compatibility Example

Static and Dynamic Types

Type Example

The Object Class

Object Polymorphism

Casting

Object-Casting Problems

Generics

Summary


This page last modified on 11 September 2009.

Creative
    Commons License