Chapter 2: Abstract Data Types (ADTs)

Abstraction

Definition

  • A fundamental concept in computer science and software engineering that involves simplifying complex systems by hiding unnecessary details and exposing only the essential features.

Causes

  • Not specified in notes

Goals / Objectives

  • To manage complexity
  • To enhance understanding
  • To improve the efficiency of software development and usage

Importance

  • Helps manage the complexity of software systems, making them easier to design, develop, and maintain
  • Provides a common language for developers, designers, and stakeholders to communicate effectively about system functionality and design

Procedures

  • Not specified in notes

Advantages & Disadvantages

  • Advantages:
    • Enables focus on abstract properties without worrying about implementation
    • Promotes code reuse by allowing general-purpose components
    • Helps isolate changes—implementation details can change while the high-level interface remains the same
  • Disadvantages:
    • Not specified in notes

Impact / Effect

  • Reduces cognitive load during design and development
  • Increases software quality and developer productivity

Examples

  • Not specified in notes

Encapsulation

Definition

  • A principle that allows using components without knowing their implementation details; achieved through information hiding.

Causes

  • Not specified in notes

Goals / Objectives

  • To hide implementation details of data structures
  • To allow controlled access to data through public methods

Importance

  • Critical for achieving code maintainability and reuse
  • Supports modularity by isolating internal changes from external usage

Procedures

  • In object-oriented programming (e.g., Java), encapsulation is achieved by:
    • Making data fields private
    • Providing public methods for controlled access and manipulation of data fields

Advantages & Disadvantages

  • Advantages:
    • Enhances maintainability—changes to implementation don’t affect client code
    • Promotes secure and predictable interaction with data
  • Disadvantages:
    • Not specified in notes

Impact / Effect

  • If a bug is found in an encapsulated component, fixes only need to be applied in one place
  • Prevents code duplication across applications

Examples

  • MinionSoft’s repeated list implementations across projects (e.g., playlist in BananaBananana MP5 player, weapon list in MonsterMinion game, task list in OhPotato! app) illustrate the need for encapsulation to avoid redundant and hard-to-maintain code

Abstract Data Types (ADTs)

Definition

  • An ADT specifies the operations that can be performed on data and the rules for these operations, without specifying how the operations are implemented.

Causes

  • Not specified in notes

Goals / Objectives

  • To define data types by their behavior rather than implementation
  • To support abstraction and encapsulation in software design
  • To enable modularity and separation of concerns

Importance

  • Allows client programs to use data structures without depending on their internal implementation
  • Makes it possible to change or improve implementations without affecting code that uses the ADT

Procedures

  • Creating an ADT involves three steps:
    1. Write the ADT specification: Describe logical properties and operations without implementation or usage details
    2. Implement the ADT:
      • Write a Java interface containing all operations from the specification
      • Write a Java class that implements the interface, defines data representation, and implements all operations
    3. Use the ADT in a client program or application

Advantages & Disadvantages

  • Advantages:
    • Increases code reuse
    • Improves maintainability
    • Enhances reliability (bug-free systems)
    • Facilitates project completion on time and handling of more projects
  • Disadvantages:
    • Not specified in notes

Impact / Effect

  • Reduces code duplication (e.g., MinionSoft no longer needs to rewrite list logic for every project)
  • Simplifies bug fixes—changes are localized to the ADT implementation
  • Promotes modular, scalable, and robust software design

Examples

  • The Counter ADT:
    • Represents a non-negative integer used for counting (e.g., cars in a parking lot, people at a post office)
    • Operations: read(), reset(), increment(), decrement()
    • Implemented in Java via CounterInterface.java and Counter.java
    • Used in a GUI application CarParkSystem.java simulating a two-wing car park

ADT Specification

Definition

  • The definition and description of an ADT, focusing on its behavior and operations rather than implementation details; written in natural language and independent of any programming language.

Causes

  • Not specified in notes

Goals / Objectives

  • To serve as a contract or blueprint for concrete data types
  • To clearly communicate what an ADT does without revealing how it does it

Importance

  • Acts as a foundation for implementation and usage
  • Ensures consistency between design and code

Procedures

  • An ADT specification should include:
    • ADT title
    • Description of logical properties of the data type
    • For each operation:
      • Operation header (return type, name, parameters)
      • Brief description of what it does
      • Precondition: condition(s) that must be true before the operation is invoked
      • Postcondition: what is true after the operation completes
      • Return value (if any)

Advantages & Disadvantages

  • Advantages:
    • Language-agnostic—can be understood by all stakeholders
    • Guides correct implementation and usage
  • Disadvantages:
    • Not specified in notes

Impact / Effect

  • Enables clear separation between interface and implementation
  • Supports team collaboration and long-term maintenance

Examples

  • ADT Counter specification includes:
    • Integer read(): returns current value; object unchanged
    • reset(): sets value to 0
    • increment(): increases value by 1
    • decrement(): decreases value by 1
    • Each includes description and postcondition (preconditions not explicitly listed but implied, e.g., counter value ≥ 0)

Java Implementation of ADTs

Definition

  • The process of translating an ADT specification into executable code using Java interfaces and classes.

Causes

  • Not specified in notes

Goals / Objectives

  • To realize the abstract design in a concrete, usable form
  • To enforce encapsulation and abstraction through language features

Importance

  • Bridges theoretical design (ADT spec) with practical application
  • Enables reuse across multiple client programs

Procedures

  • Two-step implementation in Java:
    1. Create a Java interface that mirrors the ADT specification (method signatures only)
    2. Create a class that:
      • Implements the interface
      • Defines private data fields (encapsulation)
      • Provides method implementations (actual logic)

Advantages & Disadvantages

  • Advantages:
    • Leverages Java’s support for interfaces and access control
    • Ensures client code depends only on the interface, not implementation
  • Disadvantages:
    • Not specified in notes

Impact / Effect

  • Client programs (e.g., CarParkSystem.java) interact only with the interface
  • Implementation can be swapped (e.g., for performance or testing) without changing client code

Examples

  • Files in Chapter2\carpark\:
    • CounterInterface.java: defines the ADT contract
    • Counter.java: implements the interface with private integer field and public methods
    • CarParkSystem.java: GUI client that uses the Counter ADT for simulating car counts in two wings

Benefits of ADTs (Summarized Concepts)

Definition

  • Not specified in notes (this is a synthesis of prior content)

Causes

  • Not specified in notes

Goals / Objectives

  • To increase productivity and assure quality in software projects

Importance

  • Central to professional software engineering practices
  • Directly supports winning strategies in programming (on-time delivery, handling more projects, reliability)

Procedures

  • Achieved through:
    • Abstraction → for reuse
    • Encapsulation / Information Hiding → for maintainability

Advantages & Disadvantages

  • Advantages:
    • Manage Complexity: focus on high-level design
    • Enhance Reusability: build once, use everywhere
    • Improve Maintainability: isolate changes
    • Facilitate Communication: shared conceptual language
  • Disadvantages:
    • Not specified in notes

Impact / Effect

  • Transforms ad-hoc coding into systematic, scalable development
  • Turns repeated, error-prone patterns (like MinionSoft’s list duplication) into reliable, centralized components

Examples

  • MinionSoft’s realization that all 7 projects used some form of list led to the insight that an ADT for lists would solve duplication and maintenance issues