Abstract Classes
With Examples in Java |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
abstract
modifier,
couldn't we just not have constructors?abstract
modifier,
couldn't we just make the constructors private?abstract
modifier is more direct/straightforward
private
constructors are used for other purposes
along with factory methods (more on this later)
AbstractContent
Classimport 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; } }
Object
which is concrete)
SecurityQuotation
Classimport 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; } }
toString()
method sends
the getSymbol()
message to this
(i.e., the abstract getSymbol()
method
is called in the concrete toString()
method)this
refers to an object that was instantiated,
so it must be an element of a concrete class, it must
have an implementation
StockQuotation
Derived Class
This class adds a ticker symbol attribute. Since it is a concrete class,
it must implement the getSymbol()
method.
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; } }
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.
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); } }
abstract
and static
since an abstract method must be
bound dynamically (and, as you can tell from the
modifier, static methods aren't)