Docsity

Abstract Data Types: Modularity, Procedural Abstraction, and Information Hiding, Papers of Data Structures and Algorithms

Toggle sidebar
bg1
1
©2004 Pears on Addison-Wesley. All rights res erved 3-1
Abstract Data Types
• Modularity
– Keeps the complexity of a large program manageable
by systematically controlling the interaction of its
components
– Isolates errors
– Eliminates redundancies
– A modular program is
• Easier to write
• Easier to read
• Easier to modify
©2004 Pears on Addison-Wesley. All rights res erved 3-2
Abstract Data Types
• Procedural abstraction
– Separates the purpose and use of a module from its
implementation
– A module’s specifications should
• Detail how the module behaves
• Identify details that can be hidden within the module
• Information hiding
– Hides certain implementation details within a module
– Makes these details inaccessible from outside the
module
©2004 Pears on Addison-Wesley. All rights res erved 3-3
Abstract Data Types
Figure 3.1
Figure 3.1
Isolated tasks: the implementation of task Tdoes not affect task Q
©2004 Pears on Addison-Wesley. All rights res erved 3-4
Abstract Data Types
• The isolation of modules is not total
– Methods’ specifications, or contracts, govern how they interact
with each other
Figure 3.2
Figure 3.2
A slit in the wall
©2004 Pears on Addison-Wesley. All rights res erved 3-5
Abstract Data Types
• Typical operations on data
– Add data to a data collection
– Remove data from a data collection
– Ask questions about the data in a data collection
• Data abstraction
– Asks you to think what you can do to a collection of
data independently of how you do it
– Allows you to develop each data structure in relative
isolation from the rest of the solution
– A natural extension of procedural abstraction
©2004 Pears on Addison-Wesley. All rights res erved 3-6
Abstract Data Types
• Abstract data type (ADT)
– An ADT is composed of
• A collection of data
• A set of operations on that data
– Specifications of an ADT indicate
• What the ADT operations do, not how to implement
them
– Implementation of an ADT
• Includes choosing a particular data structure
pf3
pf4
pf5

Partial preview of the text

Download Abstract Data Types: Modularity, Procedural Abstraction, and Information Hiding and more Papers Data Structures and Algorithms in PDF only on Docsity!

© 2004 Pearson Addison-Wesley. All rights reserved 3-

Abstract Data Types

  • Modularity
    • Keeps the complexity of a large program manageable by systematically controlling the interaction of its components
    • Isolates errors
    • Eliminates redundancies
    • A modular program is
      • Easier to write
      • Easier to read
      • Easier to modify

© 2004 Pearson Addison-Wesley. All rights reserved 3-

Abstract Data Types

  • Procedural abstraction
    • Separates the purpose and use of a module from its implementation
    • A module’s specifications should
      • Detail how the module behaves
      • Identify details that can be hidden within the module
  • Information hiding
    • Hides certain implementation details within a module
    • Makes these details inaccessible from outside the module

© 2004 Pearson Addison-Wesley. All rights reserved 3-

Abstract Data Types

Figure 3.1Figure 3. Isolated tasks: the implementation of task T does not affect task Q © 2004 Pearson Addison-Wesley. All rights reserved 3-

Abstract Data Types

  • The isolation of modules is not total
    • Methods’ specifications, or contracts, govern how they interact with each other

Figure 3.2 Figure 3. A slit in the wall

Abstract Data Types

  • Typical operations on data
    • Add data to a data collection
    • Remove data from a data collection
    • Ask questions about the data in a data collection
  • Data abstraction
    • Asks you to think what you can do to a collection of data independently of how you do it
    • Allows you to develop each data structure in relative isolation from the rest of the solution
    • A natural extension of procedural abstraction

Abstract Data Types

• Abstract data type (ADT)

  • An ADT is composed of
    • A collection of data
    • A set of operations on that data
  • Specifications of an ADT indicate
    • What the ADT operations do, not how to implement them
  • Implementation of an ADT
    • Includes choosing a particular data structure

© 2004 Pearson Addison-Wesley. All rights reserved 3-

Abstract Data Types

  • Data structure
    • A construct that is defined within a programming language to store a collection of data
    • Example: arrays
  • ADTs and data structures are not the same
  • Data abstraction
    • Results in a wall of ADT operations between data structures and the program that accesses the data within these data structures

© 2004 Pearson Addison-Wesley. All rights reserved 3-

Abstract Data Types

Figure 3.4Figure 3. A wall of ADT operations isolates a data structure from the program that uses it

© 2004 Pearson Addison-Wesley. All rights reserved 3-

Specifying ADTs

  • In a list
    • Except for the first and last items, each item has - A unique predecessor - A unique successor
    • Head or front
      • Does not have a predecessor
    • Tail or end
      • Does not have a successor Figure 3.5Figure 3. list A grocery © 2004 Pearson Addison-Wesley. All rights reserved 3-

The ADT List

  • ADT List operations
    • Create an empty list
    • Determine whether a list is empty
    • Determine the number of items in a list
    • Add an item at a given position in the list
    • Remove the item at a given position in the list
    • Remove all the items from the list
    • Retrieve (get) the item at a given position in the list
  • Items are referenced by their position within the list

The ADT List

• Specifications of the ADT operations

  • Define the contract for the ADT list
  • Do not specify how to store the list or how to perform the operations

• ADT operations can be used in an

application without the knowledge of how

the operations will be implemented

The ADT List

Figure 3.6Figure 3. The wall between displayList and the implementation of the ADT list

© 2004 Pearson Addison-Wesley. All rights reserved 3-

Implementing ADTs

Figure 3.8Figure 3. Violating the wall of ADT operations © 2004 Pearson Addison-Wesley. All rights reserved 3-

Java Classes

  • Object-oriented programming (OOP) views a program as a collection of objects
  • Encapsulation
    • A principle of OOP
    • Can be used to enforce the walls of an ADT
    • Combines an ADT’s data with its method to form an object
    • Hides the implementation details of the ADT from the programmer who uses it

© 2004 Pearson Addison-Wesley. All rights reserved 3-

Java Classes

Figure 3.9Figure 3. An object’s data and methods are encapsulated

© 2004 Pearson Addison-Wesley. All rights reserved 3-

Java Classes

• A Java class

  • A new data type whose instances are objects
  • Class members
    • Data fields
      • Should almost always be private
    • Methods
  • All members in a class are private, unless the programmer designates them as public

Java Classes

• A Java class (Continued)

  • Constructor
    • A method that creates and initializes new instances of a class
    • Has the same name as the class
    • Has no return type
  • Java’s garbage collection mechanism
    • Destroys objects that a program no longer references

Java Classes

• Constructors

  • Allocate memory for an object and can initialize the object’s data
  • A class can have more than one constructor
  • Default constructor
    • Has no parameters
    • Typically, initializes data fields to values the class implementation chooses

© 2004 Pearson Addison-Wesley. All rights reserved 3-

Java Classes

• Constructors (Continued)

  • Compiler-generated default constructor
    • Generated by the compiler if no constructor is included in a class

• Client of a class

  • A program or module that uses the class

© 2004 Pearson Addison-Wesley. All rights reserved 3-

Java Classes

• Inheritance

  • Base class or superclass
  • Derived class or subclass
    • Inherits the contents of the superclass
    • Includes an extends clause that indicates the superclass
    • super keyword
      • Used in a constructor of a subclass to call the constructor of the superclass

© 2004 Pearson Addison-Wesley. All rights reserved 3-

Java Classes

• Object Equality

  • equals method of the Object class
    • Default implementation
      • Compares two objects and returns true if they are actually the same object
    • Customized implementation for a class
      • Can be used to check the values contained in two objects for equality

© 2004 Pearson Addison-Wesley. All rights reserved 3-

Java Interfaces

• An interface

  • Specifies methods and constants, but supplies no implementation details
  • Can be used to specify some desired common behavior that may be useful over many different types of objects
  • The Java API has many predefined interfaces
    • Example: java.util.Collection

Java Interfaces

  • A class that implements an interface must
    • Include an implements clause
    • Provide implementations of the methods of the interface
  • To define an interface
    • Use the keyword interface instead of class in the header
    • Provide only method specifications and constants in the interface definition

Java Exceptions

• Exception

  • A mechanism for handling an error during execution
  • A method indicates that an error has occurred by throwing an exception

© 2004 Pearson Addison-Wesley. All rights reserved 3-

An Array-Based Implementation

of the ADT List

Figure 3.10Figure 3. An array-based implementation of the ADT list © 2004 Pearson Addison-Wesley. All rights reserved 3-

Summary

  • Data abstraction: a technique for controlling the interaction between a program and its data structures
  • An ADT: the specifications of a set of data management operations and the data values upon which they operate
  • The formal mathematical study of ADTs uses systems of axioms to specify the behavior of ADT operations
  • Only after you have fully defined an ADT should you think about how to implement it

© 2004 Pearson Addison-Wesley. All rights reserved 3-

Summary

  • A client should only be able to access the data structure by using the ADT operations
  • An object encapsulates both data and operations on that data - In Java, objects are instances of a class, which is a programmer-defined data type
  • A Java class contains at least one constructor, which is an initialization method
  • Typically, you should make the data fields of a class private and provide public methods to access some or all of the data fields