Using an Abstract Class
- Recognize the use of abstract classes and abstract methods to provide code reuse as well as a common interface among subtypes.
- Identify the syntax of Java Abstract Classes.
Now we look at a better design where Roster is an abstract class:
 
Here is how the abstract Roster is defined: 
public abstract class Roster {
  protected Student[] students;
  protected int numStudents;
  public Roster(int size) {
    students = new Student[size];
    numStudents = 0;
  }
  public abstract void add(Student s);
  public abstract void remove(Student s);
  public abstract Student find(String email);
}
An abstract class is declared with the abstract keyword. It signals that it cannot be instantiated (even though it has a constructor).
An abstract class typically contains one or more abstract methods.
An abstract method is a method that is declared with the abstract keyword and without an implementation.
Instead of curly brackets ({ }), an abstract method's signature ends with a semicolon (;).
Any (non-abstract) sub-class that extends an abstract class is responsible for implementing its abstract methods.
Exercise Why is the design above better than those presented earlier?
Solution
- 
It defines semantically sound "is-a" relationships. 
- 
We can leverage from type substitution. For example, we can have an array of type Rosterand store rosters of typeJhuRosterandMoocRosterin there.
- 
Rosterdoes not implement the core operations. Yet, it provides a unifying signature. The sub-classes must adhere to the "interface" (method signatures of core operations) provided by theRoster.
- 
Rostercannot be instantiated directly (which gets around the problem of declaring it as a regular class and leaving implementations as stubs).
Aside: An example of a (non-abstract) method that can be implemented in the abstract class Roster is 
public int getNumStudents() {
  return numStudents;
}
This method can be used by all classes that extend Roster.
Resources
- The BeginnersBook has an article, Abstract Class in Java with example, which you may find helpful to review.