Abstract Class (Java)
An abstract class is a class that can't be instantiated on its own and is meant to be extended. It can mix fully written methods with abstract methods that subclasses must complete.
Java
Use an abstract class when several related classes share common code but each must also provide its own version of certain behavior. You mark the class and its incomplete methods with the abstract keyword.
Unlike an interface, an abstract class can hold fields and provide method bodies, but a class can only extend one of them.
Examples
Java
abstract class Animal {
abstract String speak();
String describe() { return "I say " + speak(); }
}
class Dog extends Animal {
String speak() { return "woof"; }
}
public class Main {
public static void main(String[] args) {
System.out.println(new Dog().describe());
}
}Output:
I say woof