Abstract Data Type
Abstract Data Type
Mahender K
An abstract data type (ADT) is a type name and a set of operations on that type where:
Users of the ADT are expected to alter/examine values
of this type only via the operations provided. The creator of the ADT promises to leave the operation specifications unchanged. The creator of the ADT is allowed to change the code of the operations at any time, as long as it continues to satisfy the specifications. The creator of the ADT is also allowed to change the data structure actually used to implement the type.
10/2/2009 Data Structures in Java 2
An ADT is not a data type(DT), because it has no representation. We implement an ADT by supplying a representation for the data and algorithms for the operations. So an ADT implementation is a data type. Another way of looking at it:
An ADT is a design concept. A DT is a programming language concept. An ADT implementation is the combination of the
two.
10/2/2009
10/2/2009
can be read.
10/2/2009
/** Interface for counting objects. */ public interface Countable { /** Increment the internal counter. */ public void inc(); /** Reset the internal counter to 0. */ public void resetCount(); /** Return the internal count. */ public int readCount(); }
10/2/2009
10/2/2009
/** Simple counting objects. */ public class SimpleCounter implements Countable { /** Initialization for new counters. */ public SimpleCounter () { resetCount(); } /** Reset the counter to 0. */ public void resetCount () { count = 0; } Constructor /** Increment the counter. */ Method public void inc () { count++; } /** Return the current count. */ public int readCount () { return count; } private int count; }
Private instance variable
10/2/2009
/** A program that uses a SimpleCounters.*/ public class Main { public static void main (String args[]) { int n = 99; SimpleCounter c = new SimpleCounter(); SimpleCounter d = new SimpleCounter(); c.inc(); d.inc(); d.inc(); System.out.println (n + c.readCount() + d.readCount()); } Creation of new }
SimpleCounter objects.
10/2/2009
10/2/2009
10
10/2/2009
11
Strictly speaking, Java does not require us to make use of an interface for this program.
10/2/2009
12
Strictly speaking, Java does not require us to make use of an interface for this program.
public class SimpleCounter { } Leaving out the implements declaration is OK with Java.
10/2/2009
13
Interfaces allow us to separate interface from implementation. Clients assume only the interface. Clients thereby protect themselves from many kinds of changes in the underlying implementations.
10/2/2009
14
10/2/2009
15
10/2/2009
16
Separate interface from implementation. Clients do not need to know most implementation details.
10/2/2009
17
Abstract data types make it much easier to make use of pre-existing code.
We shall see this in later assignments in the
project
work in a team. maintain code in the long run. write down the results of your careful thinking about the problem.
Data Structures in Java 18
10/2/2009
/** Counting objects that double every time. */ public class DoublingCounter implements Countable { /** Initialization for new counters. */ public DoublingCounter () { resetCount(); } /** Reset the counter to 1. */ public void resetCount () { count = 1; }
/** Increment the counter. */ public void inc () { count *= 2; } /** Return the current count. */ public int readCount () { return count; } private int count; }
10/2/2009 Data Structures in Java 19
10/2/2009