Abstract Classes
- 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.
An abstract class is just like a regular (non-abstract) class except that:
- It contains the keyword
abstract
in its declaration, and - It cannot be instantiated.
So, in the code snippet below, the last statement will cause a compiler error:
Roster cs226 = new JhuRoster(100); // fine
Roster ep202 = new MoocRoster(1000); // fine
Roster myRoster = new Roster(50); // compiler error
In other words, while an abstract class can be used as the apparent type of an object, it cannot be used as its actual type.
An abstract class may contain fields, constructors, and fully implemented methods, just like a regular class.
An abstract class usually contains abstract methods, but it does not have to! On the other hand, a regular class cannot include abstract methods. So if you have an abstract method, it must be inside of an abstract class.
An abstract class can be used as a parent class or extend another (regular or abstract) class.
In programming languages that support object-oriented paradigm, there almost always is a construct similar to Java's abstract class (see Abstract type on Wikipedia).
In C++, for instance, an abstract class is one that has one or more pure virtual functions (the same idea as abstract methods in Java).