Abstract classes are classes declared with abstract
. They can be subclassed or extended, but cannot be instantiated. You can think of them as a class version of interfaces, or as an interface with actual code attached to the methods.
For example, say you have a class Vehicle
which defines the basic functionality (methods) and components (object variables) that vehicles have in common.
You cannot create an object of Vehicle
because a vehicle is itself an abstract, general concept. Vehicles have wheels and motors, but the number of wheels and the size of motors can differ greatly.
You can, however, extend the functionality of the vehicle class to create a Car
or a Motorcycle
:
abstract class Vehicle
{
//variable that is used to declare the no. of wheels in a vehicle
private int wheels;
//Variable to define the type of motor used
private Motor motor;
//an abstract method that only declares, but does not define the start
//functionality because each vehicle uses a different starting mechanism
abstract void start();
}
public class Car extends Vehicle
{
...
}
public class Motorcycle extends Vehicle
{
...
}
Remember, you cannot instantiate a Vehicle
anywhere in your program – instead, you can use the Car
and Motorcycle
classes you declared earlier and create instances of those:
Vehicle newVehicle = new Vehicle(); // Invalid
Vehicle car = new Car(); // valid
Vehicle mBike = new Motorcycle(); // valid
Car carObj = new Car(); // valid
Motorcycle mBikeObj = new Motorcycle(); // valid