An abstract class in Java is one that is intended to be subclassed by other classes and cannot be instantiated on its own. It can have concrete methods, abstract methods, or a mix of both; it acts as a model for other classes.
**Abstract Keyword:
**The abstract keyword is used to declare an abstract class. Abstract classes may have abstract methods (methods without a body) as well as concrete methods (methods with a body).
**Abstract Methods:
**Abstract classes can have abstract methods, which are declared without a body and are meant to be implemented by the subclasses.
**Subclassing:
**When you extend an abstract class, you must either provide concrete implementations for all the abstract methods in the subclass or declare the subclass as abstract.
**Constructor:
**Abstract classes can have constructors, and they are invoked when a subclass object is created. However, abstract classes cannot be instantiated directly.
**Usage:
**Abstract classes are useful when you want to provide a common interface or functionality that should be shared among multiple related classes.
In this example, Shape is an abstract class with an abstract method draw() and a concrete method display(). The Circle class extends Shape and provides a concrete implementation of the draw() method. The display() method is inherited from the Shape class.
Top comments (0)