An abstract class is a class that cannot be used to create objects directly.
It acts like a base class that other classes can extend. Abstract classes are useful when several related classes share common behavior, but each child class may still need its own specific details.
An abstract method is a method without a body. It only defines the method name, return type, and parameters.
Any concrete subclass that extends the abstract class must provide the actual method body.
Abstract classes can also contain regular methods with a body. These are called concrete methods.
Concrete methods are useful when all child classes should share the same behavior.
You cannot write new Animal() if Animal is abstract.
Instead, you create an object from a child class that extends the abstract class. You can still use the abstract class as the reference type.
Abstract classes can have constructors, even though you cannot create objects directly from them.
The abstract class constructor runs when a subclass object is created. This helps initialize shared data from the parent class.
Abstract classes and interfaces both support abstraction, but they are used in different situations.
Use an abstract class when related classes share common data or common code. Use an interface when different classes should follow the same behavior rule.
Abstract classes help you create a shared structure for related classes while still forcing child classes to define their own specific behavior.
For example, every Shape can calculate area, but a Circle and a Rectangle calculate area differently.
Create an abstract class called Shape with an abstract method calculateArea(). Then create Circle and Rectangle classes that extend Shape and calculate their areas.