abstract Keyword in Java
Last Updated : 18 Feb 2026
The Java abstract keyword is a non-access modifier used with classes and methods to achieve abstraction.
Purpose of abstract Keyword
The abstract keyword facilitates abstraction by allowing us to define a blueprint or a contract for classes without providing complete implementation details. It promotes code reusability and enforces a specific structure and behavior for subclasses, ensuring that they implement certain methods.
Characteristics of abstract Keyword
- The abstract keyword can be used to declare abstract classes and abstract methods.
- We cannot create an object of an abstract class directly. It is meant to be extended by subclasses.
- Abstract methods do not have a body (i.e., no implementation). The method ends with a semicolon: abstract void draw();
- Abstract classes can implement interfaces, which define a set of methods that must be implemented by any class that implements the interface.
- An abstract class can include both abstract methods and concrete (regular) methods with implementations.
- Abstract classes can have instance variables, constructors, and even static methods. It can be used by both the abstract class and its subclasses. Subclasses can access these variables directly.
- An abstract keyword cannot be used with variables and constructors.
- We cannot use the abstract keyword with the final.
Usage of abstract Keyword
The abstract keyword is used to define abstract classes and abstract methods.
Abstract Class
An abstract class is a class that cannot be instantiated (you cannot create objects of it) and may contain abstract methods (methods without a body). For example, java.lang.Number class is an abstract class.
- An abstract class can contain constructors and static methods.
- An abstract class can contain overloaded abstract methods.
- We can declare the local inner class as abstract.
- We can declare the abstract method with a throw clause.
To read more Abstract Class in Java
Syntax:
Abstract Methods
A method declared using the abstract keyword within an abstract class and does not have a definition (implementation) is called an abstract method. When we need just the method declaration in a super class, it can be achieved by declaring the methods as abstract.
- We cannot declare abstract methods as private.
- We cannot declare abstract methods as static.
- An abstract method can't be synchronized.
To read more Abstract Method in Java
Note: We cannot declare abstract methods in a non-abstract class.
Syntax:
Example of abstract Keyword
Using abstract Keyword with Class and Method
Output:
Dog barks
This animal eats food.
Using Abstract Class with Multiple Subclasses
Output:
Drawing Circle
Drawing Square
abstract Keyword Best Practice
- Abstract Classes for Shared Code: When shared code is required, use abstract classes so that multiple subclasses can inherit that code.
- Mandatory Implementation: Implement all the abstract class methods in subclasses.
- Abstract Methods for Interfaces: If we have a class that acts as a base and requires methods to be overridden, use abstract methods to enforce this contract.
- Cannot Instantiate: Creating an instance of an abstract class is not allowed directly. So, we must create an instance of a subclass that implements all abstract methods.
- Abstract Class Constructors: An abstract class can have constructors, which can be called during the instantiation of a subclass.
- Abstract Class with Interface: An interface can be implemented with abstract classes. We can define some methods of interface in the abstract class while leaving others as abstract for implementation in subclasses.
- Static Members: An abstract class can have static members that can be accessed without creating an instance of the abstract class.