JMU
Abstract Classes
With Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Review
The Basics of Abstract Classes
The Basics of Abstract Classes (cont.)
Concrete and Abstract Methods
Abstract Methods
Concrete Methods
Interfaces vs. Abstract Classes
UML Details
Java Details
A Simple Example
Do We Need Abstract Classes?
Constructors in Abstract Classes
A Simple Example (cont.)

The AbstractContent Class

javaexamples/oopbasics/AbstractContent.java
import java.util.*;

/**
 * An abstract "piece of" content that will be extended 
 * to create concrete media (e.g., Image, VideoClip)
 *
 * @version 1.0
 * @author  Prof. David Bernstein, James Madison University
 */
public abstract class AbstractContent
{
    private Date   creationDate;
    private String author, title;



    /**
     *  "Constructs" a new AbstractContent
     *  (Note: This method should be called by the 
     *  constructor of all child classes)
     */
    protected AbstractContent()
    {
        creationDate = new Date();
    }

    /**
     * Returns the author of this AbstractContent
     */
    public String getAuthor()
    {
        return author;
    }

    /**
     * Returns the creation date
     */
    public Date getCreationDate()
    {
        return creationDate;
    }

    /**
     * Returns the title of this AbstractContent
     */
    public String getTitle()
    {
        return title;
    }


    /**
     *  Show/play/render this AbstractContent
     */
    public abstract void show();


    /**
     * Sets the author of this AbstractContent
     */
    public void setAuthor(String author)
    {
        this.author = author;
    }


    /**
     * Sets the title of this AbstractContent
     */
    public void setTitle(String title)
    {
        this.title = title;
    }
}
        
Abstract Classes in a Hierarchy
A More Complete Example
A More Complete Example (cont.)

The Abstract SecurityQuotation Class

javaexamples/oopbasics/finance/SecurityQuotation.java
import java.util.Calendar;
import java.util.GregorianCalendar;


/**
 * An abstract SecurityQuotation (extended by StockQuotation, 
 * FutureQuotation, etc...).
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public abstract class SecurityQuotation
{
    protected GregorianCalendar   date;
    protected double              open, high, low, close;
    protected int                 volume;
    

    /**
     * Explicit Value Constructor.
     *
     * @param date      The date of the entry
     * @param open      The opening price
     * @param high      The high price
     * @param low       The low price
     * @param close     The closing price
     * @param volume    The volume traded
     */
    protected SecurityQuotation(GregorianCalendar date, 
                                double open,double high,double low,double close,
                                int volume)
    {
       this.date = date;
       this.open = open;
       this.high = high;
       this.low = low;
       this.close = close;
       this.volume = volume;
    }
    
    /**
     * Return the closing price.
     *
     * @return    The closing price
     */
    public double getClose()
    {
       return close;
    }

    /**
     * Return the date.
     *
     * @return    The date of this quotation
     */
    public GregorianCalendar getDate()
    {
       return date;
    }

    /**
     * Return the high price.
     *
     * @return    The high price
     */
    public double getHigh()
    {
       return high;
    }

    /**
     * Return the low price
     *
     * @return    The low price
     */
    public double getLow()
    {
       return low;
    }

    /**
     * Return the opening price.
     *
     * @return    The opening price
     */
    public double getOpen()
    {
       return open;
    }

    /**
     * Return the ticker symbol.
     */
    public abstract String getSymbol();
        

    /**
     * Returns the volume.
     *
     * @return    The volume
     */
    public int getVolume()
    {
       return volume;
    }

    /**
     * Return a String representation.
     *
     * @return   The String representation
     */
    public String toString()
    {
        return getSymbol() + "\t" +
            (date.get(Calendar.MONTH)+ 1) + "/" +
            date.get(Calendar.DAY_OF_MONTH) + "/" +
            date.get(Calendar.YEAR) +
            "\tO: " + open + "\tH: " + high +
            "\tL: " + low + "\tC: " + close + 
            "\tV: " + volume;
    }
}
        
A More Complete Example (cont.)
A More Complete Example (cont.)

The Concrete StockQuotation Derived Class

This class adds a ticker symbol attribute. Since it is a concrete class, it must implement the getSymbol() method.

javaexamples/oopbasics/finance/StockQuotation.java
import java.util.GregorianCalendar;


/**
 * A stock quotation (i.e., open, high, low, close and  volume).
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class StockQuotation extends SecurityQuotation
{
    protected String           symbol;


    /**
     * Explicit Value Constructor.
     *
     * @param symbol    The ticker symbol of the stock
     * @param date      The date of the entry
     * @param open      The opening price
     * @param high      The high price
     * @param low       The low price
     * @param close     The closing price
     * @param volume    The volume traded
     */
    public StockQuotation(String symbol, GregorianCalendar date, 
                          double open,double high,double low,double close,
                          int volume)
    {
        super(date, open, high, low, close, volume);
        this.symbol = symbol;
    }

    /**
     * Return the ticker symbol (required by SecurityQuotation).
     *
     * @param symbol     The ticker symbol
     */
    public String getSymbol()
    {
        return symbol;
    }
}
        
A More Complete Example (cont.)

The Concrete FutureQuotation Derived Class

This class adds an attribute for the open interest, the code for the commodity, and the month and year of the contract. It also adds an accessor for the open interest and, since it is concrete, implements the getSymbol() method.

javaexamples/oopbasics/finance/FutureQuotation.java
import java.util.GregorianCalendar;

/**
 * A future's contract quotation (i.e., open, high, low, close,
 * volume, and open interest).
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class FutureQuotation extends SecurityQuotation
{
    protected int              openInterest;
    protected String           commodity, month, year;

    /**
     * Explicit Value Constructor.
     *
     * @param commodity The commodity code
     * @param year      The year code
     * @param month     The month code
     * @param date      The date of the entry
     * @param open      The opening price
     * @param high      The high price
     * @param low       The low price
     * @param close     The closing price
     * @param volume    The volume traded
     */
    public FutureQuotation(String commodity, String year, String month, 
                           GregorianCalendar date, 
                           double open,double high,double low,double close,
                           int volume, int openInterest)
    {
        super(date, open, high, low, close, volume);

        this.commodity    = commodity;
        this.year         = year;
        this.month        = month;
        this.openInterest = openInterest;
    }

    /**
     * Return the open interest for this security.
     *
     * @return  The open interest
     */
    public int getOpenInterest()
    {
        return openInterest;
    }

    /**
     * Return the symbol for this security.
     *
     * @param symbol     The ticker symbol for this stock
     */
    public String getSymbol()
    {
        return commodity + year + month;        
    }

    /**
     * Return a String representation.
     *
     * @return   The String representation
     */
    public String toString()
    {
        return (super.toString()+"\tOI: " + openInterest);
    }
}
        
Some Important Consequences