- Forward


Designing with Abstract Classes and Interfaces
an Example in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

A Review of Abstract Classes
Back SMYC Forward
  • Definition:
    • An abstract class is a class that can't have any instances (i.e., one can't, even conceptually, create instances of an abstract class)
  • An Important Organizational Rationale:
    • Abstract classes can be used to reduce code duplication (i.e., if multiple classes require the same functionality then it can be put in an abstract parent)
A Review of Interfaces
Back SMYC Forward
  • Basics:
    • An interface provides a view of a set of services provided by a class
    • A class that realizes an interface promises that it will implement all of the methods in the interface
  • Uses:
    • Describing similarities between classes that are not related hierarchically
    • Revealing the capabilities of an object without revealing its class (sometimes called anonymous objects)
Design Issues
Back SMYC Forward
  • When to use an abstract class:
    • You have several classes that provide the same functionality in the same way but you do not want to move that functionality into a concrete parent because it doesn't make sense to have instances of the parent
  • When to use an interface:
    • You have classes that provide the same functionality in different ways and/or are not related hierachically
    • You need to reveal the capabilities of an object without revealing its class
An Example
Back SMYC Forward
  • Objective:
    • Design and implement a "calculator" that can operate on a variety of different kinds of objects
  • Process:
    1. Start with integer values to gain some intuition
    2. Determine the capabilities required of other kinds of objects
    3. Create the necessary interfaces
    4. Implement the interfaces
    5. Create an abstract class that can capture the generic functionality of a calculator
    6. Create concrete specializations of the abstract calculator that can operate on specific kinds of objects
Getting Started
Back SMYC Forward

A Simple Integer Calculator

javaexamples/oopbasics/calculator/IntegerCalculator.java
 
Getting Started (cont.)
Back SMYC Forward

Steps in the Process

  1. Tokenize the expression
  2. Create objects for the operands
  3. Initialize the operands from String representations
  4. Perform the operation (from here on, addition only)

Requirments of Operands

  • Initialize the operands from String representations -- Initializeable
  • Perform the addition operation -- Increaseable
The Interfaces
Back SMYC Forward
javaexamples/oopbasics/calculator/Initializeable.java
 
javaexamples/oopbasics/calculator/Increaseable.java
 
javaexamples/oopbasics/calculator/Operand.java
 
Implementing Initializeable in Weight
Back SMYC Forward
javaexamples/oopbasics/calculator/Weight.java (Fragment: Initializeable)
 
Implementing Initializeable in Fraction
Back SMYC Forward
javaexamples/oopbasics/calculator/Fraction.java (Fragment: Initializeable)
 
Implementing Increaseable in Weight
Back SMYC Forward
javaexamples/oopbasics/calculator/Weight.java (Fragment: Increaseable)
 
Implementing Increaseable in Fraction
Back SMYC Forward
javaexamples/oopbasics/calculator/Fraction.java (Fragment: Increaseable)
 
A WeightAddingMachine Class
Back SMYC Forward
javaexamples/oopbasics/calculator/WeightAddingMachineBad.java
 
A FractionAddingMachine Class
Back SMYC Forward
javaexamples/oopbasics/calculator/FractionAddingMachineBad.java
 
The Need for an Abstract Class
Back SMYC Forward
  • A Defect in these Implementations:
    • There is a lot of duplicate code
  • Eliminating the Duplicate Code:
    • Move it to a common parent
  • Some Observations:
    • There is one thing that can't be done in the parent - the creation of the Operand objects
    • We can create a placeholder (i.e., an abstract method) that creates the Operand objects
    • This means the class must be abstract
An Abstract AddingMachine Class
Back SMYC Forward
javaexamples/oopbasics/calculator/AddingMachine.java
 
A Concrete WeightAddingMachine Class
Back SMYC Forward
javaexamples/oopbasics/calculator/WeightAddingMachine.java
 
A Concrete FractionAddingMachine Class
Back SMYC Forward
javaexamples/oopbasics/calculator/FractionAddingMachine.java
 
A Driver
Back SMYC Forward
javaexamples/oopbasics/calculator/Driver.java
 
Concluding Observations
Back SMYC Forward
  • A Weight object can take many forms as a result of the fact that it implements several interfaces (Increaseable, Initializeable) and as a result of the fact that it implicitly extends Object (e.g., the toString() method)
  • A Fraction object can take many forms as a result of the fact that it implements several interfaces (Increaseable, Initializeable) and as a result of the fact that it implicitly extends Object (e.g., the toString() method)
  • An AddingMachine can operate on any object that is both Initializeable and Increaseable
There's Always More to Learn
Back -