Abstract classes
Abstract classes in Java provide a high-level functionality that can be used by other classes as well. You can't create an abstract class directly, but you can implement another class that derives from the abstract class.
The simplest explanation is that an abstract class is a type of a thing, but is not a thing—by this, I mean that you can have an abstract class like our Vehicle
example that declares all of the methods that we mentioned, but you can't ever create just a vehicle—you have to have something specific, such as a car, motorbike, hovercraft, helicopter, and so on; you can't have just a generic vehicle.
All of our vehicles are slightly different, but share the same base functionality—they can go, they can stop, and they can turn. This common set of functionalities could, therefore, be modeled as base methods of an abstract (Vehicle
) class, and whenever you create a new type of vehicle, you will have all of them available to you.
To create an abstract class in Java...