The abstract keyword in Java is utilized to designate a class or a method as abstract. When applied to a class, it implies that the class itself or some of its methods do not possess a concrete implementation and need to be implemented by its subclasses. In the case of a method, it signifies that the method lacks a concrete body and must conclude with a semicolon. To illustrate, consider the following abstract class, Car:
public abstract class Car {
public abstract void drive();
}
Here, the method drive() is declared as abstract.
Consider a class, SuperCar, which extends the abstract class Car and implements its abstract method:
class SuperCar extends Car {
public void drive() {
// Implementing drive for a super car, driving faster than a normal car.
}
}
Here, SuperCar provides a concrete implementation for the abstract drive() method.
Another example involves an abstract class, Airplane, featuring both abstract and non-abstract methods:
public abstract class Airplane {
public abstract void takeOff();
public void landing() {
// Implementing a smooth landing.
}
}
In this case, Airplane contains an abstract method takeOff() and a non-abstract method landing() with a concrete implementation for a smooth landing.