- Forward


Abstract Classes
With Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • What is a class?
    • A definition of a set that is written in terms of the properties of the elements (i.e., an intensive definition of a set)
    • The set of elements so defined
    • Expand
  • What is an enum?
    • An extensive definition of a set
    • The set of elements so defined
    • Expand
  • What is an object?
    • An element of the set
    • Expand
The Basics 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 Obvious Question:
    • Why have a class if you can't create any instances of it?
  • A Conceptual Answer:
    • Definitions of things, even things that don't exist, can help us understand complex systems and organize elements of those systems
    • Expand
The Basics of Abstract Classes (cont.)
Back SMYC Forward
  • An Important Practical 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 base class)
  • A Question:
    • Isn't that what specialization is for?
  • An Answer:
    • Yes, but sometimes you want to prevent the creation of instances of the base class
    • Expand
Concrete and Abstract Methods
Back SMYC Forward
  • Concrete Methods:
    • Methods that include both a declaration and a body (i.e., an implementation)
  • Abstract Methods:
    • Methods that include only a declaration
Abstract Methods
Back SMYC Forward
  • In Abstract Classes:
    • Abstract classes may or may not contain abstract methods (i.e., methods that have not been implemented)
    • Abstract methods may be overloaded
    • Abstract methods in abstract classes may be called by concrete methods in the abstract class
  • In Concrete Classes:
    • A concrete class may not have abstract methods
Concrete Methods
Back SMYC Forward
  • In Abstract Classes:
    • In principle, abstract classes need not contain concrete methods
    • In practice, abstract classes should contain at least one concrete method (because there is a better way to achieve the goals of an abstract class with all abstract methods)
  • In Concrete Classes:
    • Concrete methods in concrete classes can implement abstract methods and override concrete methods
Interfaces vs. Abstract Classes
Back SMYC Forward
  • A Likely Question If You Know About Both:
    • Isn't an interface just an abstract class in which all of the methods are abstract?
  • An Answer:
    • They are similar, but interfaces don't involve inheritance and, hence, are simpler and more flexible
UML Details
Back SMYC Forward
  • Visualization of Abstract Classes:
    • The name of an abstract class is italicized
    • The declaration of an abstract method is italicized
  • Conventions:
    • A concrete subclass (typically) does not explicitly list the abstract methods it implements from its abstract superclass
Java Details
Back SMYC Forward
  • Syntax of an Abstract Class:
    • accessibility abstract class name { body }
  • Syntax of an Abstract Method:
    • accessibility abstract type name([parameter...]);
A Simple Example
Back SMYC Forward
  • The Setting:
    • We have a variety of different classes for different media (e.g., images, audio, video)
    • Objects in every class have an author, a creation date, and a title
    • The methods needed to manipulate these attributes do not vary from class to class
    • The method used to "show" a particular object is medium-specific
  • The Abstract Class:
    • images/abstract_content.gif
Do We Need Abstract Classes?
Back SMYC Forward
  • A Question:
    • Why do we need the abstract modifier, couldn't we just not have constructors?
  • Some Answers:
    • It's not very elegant
    • Java adds a default constructor if there are no constructors
    • Sometimes we want abstract classes to have constructors (as we'll see in a moment)
  • A Better Question:
    • Why do we need the abstract modifier, couldn't we just make the constructors private?
  • Some Answers:
    • The abstract modifier is more direct/straightforward
    • private constructors are used for other purposes along with factory methods (more on this later)
Constructors in Abstract Classes
Back SMYC Forward
  • The Seeming Contradiction:
    • A constructor is for creating instances of the classes but abstract classes can't be instantiated
  • Rationale:
    • Derived classes may need to perform the same construction-related tasks
  • Remember:
    • Constructors in derived classes should call a specific constructor in the base class (or the default constructor will be called)
A Simple Example (cont.)
Back SMYC Forward

The AbstractContent Class

javaexamples/oopbasics/AbstractContent.java
 
Abstract Classes in a Hierarchy
Back SMYC Forward
  • Descendants of Abstract Classes:
    • Abstract classes can have concrete derived classes -- a concrete derived class must implement all abstract methods in the base class
    • Abstract classes can have abstract derived classes (in fact, if a derived class doesn't implement all of the abstract methods in the base class it must be declared abstract)
  • Abstract Descendants of Concrete Classes:
    • Concrete classes can have abstract derived classes (indeed, all classes, including abstract classes, extend Object which is concrete)
A More Complete Example
Back SMYC Forward
  • The Setting:
    • Each day, all financial securities have an opening price, a high price, a low price, a closing price and a volume (the number bought and sold)
    • A "stock" is a financial security that has a ticker symbol that represents the name of the company (e.g., Microsoft's ticker symbol is MSFT)
    • A "future" is a financial security that has a ticker symbol that represents the commodity (e.g., the Australian Dollar's ticker symbol is AD) and the future delivery date (e.g., December is Z). For example, a futures contract with the symbol AD2016Z is a promise to buy/sell Australian Dollars in December of 2016.
  • The Classes in UML:
    • images/financial_classes.gif
A More Complete Example (cont.)
Back SMYC Forward

The Abstract SecurityQuotation Class

javaexamples/oopbasics/finance/SecurityQuotation.java
 
A More Complete Example (cont.)
Back SMYC Forward
  • An Important Observation:
    • The toString() method sends the getSymbol() message to this (i.e., the abstract getSymbol() method is called in the concrete toString() method)
  • What Happens?
    • At Compile Time - The compiler checks syntax (i.e., is the signature correct?)
    • At Run Time - Dynamic binding
  • Why Is This OK?
    • this refers to an object that was instantiated, so it must be an element of a concrete class, it must have an implementation
A More Complete Example (cont.)
Back SMYC Forward

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
 
A More Complete Example (cont.)
Back SMYC Forward

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
 
Some Important Consequences
Back SMYC Forward
  • Polymorphism:
    • Abstract classes must behave in exactly the same way as concrete classes
  • Methods:
    • Can't be both abstract and static since an abstract method must be bound dynamically (and, as you can tell from the modifier, static methods aren't)
There's Always More to Learn
Back -