- Forward


Interfaces and Abstract Classes
in Detailed Design


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Interfaces
Back SMYC Forward
  • Purpose:
    • Provide a view of a set of services provided by a class
    • Represent a "contract"
  • Example:
    • Sortable
    • sortable
      Expand
  • Note:
    • This is a somewhat different use of the term (as in Java)
Interfaces (cont.)
Back SMYC Forward

To Think About:

sorter
Interfaces in Java
Back SMYC Forward
  • Syntax:
    • accessibility interface name { [variables]... [methods]... }
  • File Name:
    • The name of the file containing an interface must be exactly the same as the name of the interface (with .java appended).
Interfaces in Java (cont.)
Back SMYC Forward
An Example
javaexamples/oopbasics/emergency2/Prioritized.java
 
Interfaces in Java (cont.)
Back SMYC Forward
An Example (cont.)
javaexamples/oopbasics/emergency2/EmergencyMessage.java
 
Interfaces in Java (cont.)
Back SMYC Forward
An Example (cont.)
javaexamples/oopbasics/emergency2/Alert.java
 
Interfaces in Java (cont.)
Back SMYC Forward
An Example (cont.)
javaexamples/oopbasics/emergency2/AccidentReport.java
 
Interfaces in Java (cont.)
Back SMYC Forward
An Example (cont.)
javaexamples/oopbasics/emergency2/Driver.java
 
Abstract Classes
Back SMYC Forward
  • Definition:
    • A class that cannot be instantiated (because it does not implement all of its methods)

      abstract_content

      Expand
  • Rationale:
    • Many child classes may require the functionality included in the abstract class
Abstract Classes in Java
Back SMYC Forward
  • Syntax:
    • accessibility abstract class name { [variables]... [methods]... }
  • File Name:
    • The name of the file containing an abstract class must be exactly the same as the name of the abstract class (with .java appended).
  • Abstract Methods:
    • The methods that must be implemented by "concrete" derived classes also use the abstract keyword (and have an empty body).
Abstract Classes in Java (cont.)
Back SMYC Forward
An Example
javaexamples/abstracts/AbstractContent.java
 
There's Always More to Learn
Back -