Lecture 05 ADT

Joseph Haugh

University of New Mexico

Free Recall

  • Get out a sheet of paper or open a text editor
  • For 2 minutes write down whatever comes to mind about the last class
    • This could be topics you learned
    • Questions you had
    • Connections you made

Data Abstraction Revisited

  • Built-in data types (int, boolean, etc.)
  • Programmer-defined types
  • Abstract data type (ADT)
    • A formal characterization of a set of data structures sharing a common set of operations
  • Generic types (parameterized definitions)

Abstract Data Type Definition

  • A formal characterization of a set of data structures that share a common set of operations having well-defined syntax and semantics.
  • An ADT specification:
    • Is independent of any possible realization
    • May be captured in purely mathematical terms
  • The ADT is the conceptual basis for the class construct

Basic Class Concept

  • The notion of class assumes many forms:
    • Mathematics: collection of sets sharing some property
    • Natural language concept: collection of objects sharing some properties (red, car, birds, etc.)
    • Design notation: documentation of a set of objects having identical properties
      • Does not depend on availability of an object-oriented programming language
    • Programming language construct

Class Construct in Java

  • Embodiment of the abstract data type concept
    • Fields
    • Methods
  • Mechanisms for deriving new classes:
    • Inheritance
      • Single (extending a class)
      • Multiple (implementing interfaces)
    • New fields and methods
    • Method overriding
    • Inheritance controls (final)
  • Access control mechanics: public, private, protected

Sample Class Definition

public class Asteroid {
    private static int nextid = 0;
    private int id;
    private Color color;
    private Point location;
    private int[] velocity;

    public Asteroid(Color color, Point location, int[] velocity) {
        this.color = color;
        this.location = location;
        this.velocity = velocity;
        this.id = nextid;
        nextid++;
    }
    public void updateLocation(int elapsedTime) {
    // ...
    }

    public void setVelocity(int[] velocity) {
        velocity = velocity;
    }

    public int[] getVelocity() { return velocity; }
    public Point getLocation() { return location; }
}

OOD Perspective

  • Class as a strict embodiment of the abstract data type concept
    • Private fields
    • Public methods
  • Restricted mechanisms for deriving new classes:
    • Inheritance
      • Single (extending a class)
      • Multiple (implementing interfaces)
    • Method overriding subject to semantic consistency

Object Creation

  • Objects are dynamically created instances of a class
  • Storage is allocated for the fields
  • Code is reused from the class definition
  • Java uses garbage collection to reclaim storage used by inaccessible objects

Notation for Instantiation

Simple Design Diagram