ADTs and Interfaces

Created

ADT and Interface


Abstract Class

An abstract class is declared with the abstract keyword. It signals that it cannot be instantiated (even that it has a constructor). An abstract method is a method delcared with the abstract keyword and without an implementation.

  • End the method's signature with a semicolon (;).
  • Any non-abstract sub-class that extends an abstract class is responsible for implementing its abstract methods.
  • Likely you should use the protected visibility.
  • Classes that extend this abstract class should use the @Override annotation.

An abstract class can be used as the apparent type of an object, it cannot be used as its actual type.

An interface is more abstract than an abstract class. It gives more leeway when it comes to multiple inheritance.

Abstract Data Type:

A data type consists of a type together with a collection of operations to manipulate that type. A data structure encapsulates organized mechanisms for efficiently storing, accessing, and manipulating data. An abstract data type (ADT) is a description (representation) of some type of data and the operations that are allowed on that data, while abstracting implementation details.

  • A data structure is an implementation of an ADT.
  • It provides a minimal expected interface and set of behaviors without regard to how they should be implemented.
  • It is a theoretical concept described in terms of algebraic specifications with axioms that formalize the operation and their expected behaviors.
  • To specify an ADT, we need to supplement the abstrat class consstruct with proper documentation and a suite of tests.

Interfaces

Java has a construct called Interface, which is a abstract class that contains nothing but abstract methods.

Implementation:

  • Declare with keyword interface instead of class or abstract class.
  • Does not contain fields nor constructors.
  • All methods will be public and abstract, so don't include those kewords in their declaration./
  • Interface can contain constants, default methods, static methods, and nested types.
  • A class may implement more than one interface (separated with commas), and extend a class at teh same time.
  • An interface can be a subtype of (and extend) another interface.

Documentation

Use good Javadocs. Documentation comments are considered part of the specification of an ADT.

/**
 * IndexedList ADT.
 */ 
public interface IndexedList { 
	
/**
 * Change the value at the given index. 
 *
 * @param index representing a position in this list.
 *	Pre: 0 <= index < length 
 * @param value to be written at the given index. 
 * Post: this.get(index) == value 
 */ 
void put(int index, int value); 
	
/**
 * Retrieve the value stored at the given index. 
 * 
 * @param index representing a position in this list. 
 *	Pre: 0 <= index < length 
 * @return value at the given index. 
 */ 
int get(int index); 
	
/** 
 * Get the declared capacity of this list.
 *
 * @return the length 
 * 	Inv: length() >= 0 
 */ 
int length();

Another type is algebraic documentation. Formally reason the the behavior of a method with math.

Documentation provides code contract: something you make between the interface and the clients of that interface. These can go in the javadoc.

It is a way to specify the behavior of your ADT as will as preconditions, postconditions, invarients, etc:

  • Pre: pre condition document everything that needs to be true before you call that method; the assumtions the method makes.
  • Post: post condition
  • Inv: Invariance something that is always true during the lifetime of the object.

Everytime we want to make a new data type.