Abstract class in JavaLast Updated : 3 Jan 2026 In Java programming, abstract classes are used to define the structure and behavior of classes within an inheritance hierarchy. They act as a blueprint for other classes, where some methods may be declared without implementation. In this chapter, we will learn the concept of abstract classes including their features, advantages, and examples. Before learning about abstract classes in Java, it is necessary to first understand the concept of abstraction. What is Abstraction in Java?Abstraction is the process of hiding implementation details and showing only the required functionality to the user. In other words, it displays only the essential features while hiding internal details. For example, when sending an SMS, we type the message and send it without knowing how the message is processed or delivered internally. Abstraction allows you to focus on what an object does rather than how it does it. There are two ways to achieve abstraction in Java:
What is Abstract Class in Java?A class declared using the abstract keyword is known as an abstract class in Java. It can contain both abstract methods (methods without a body) and non-abstract methods (methods with a body). An abstract class provides partial abstraction. It cannot be created on its own and is used only as a parent class. It defines a common structure and shared behavior, while child classes provide their own implementations. Abstract methods do not have a body and must be implemented by the subclasses. Points to Remember
![]() Creating / Declaring an Abstract ClassAn abstract class is created using the abstract keyword. It is used as a parent class and cannot be instantiated directly. SyntaxHere is the syntax to create an abstract class: In this example, Shape is an abstract class. It contains one abstract method area() without a body and one concrete method display() with an implementation. Any subclass of Shape must implement the area() method, while it can directly use the display() method. Abstract MethodA method that is declared using the abstract keyword and does not have an implementation (method body) is known as an abstract method. ExampleHere is an example how an abstract method looks like: An abstract method only declares the method signature. Its implementation must be provided by the subclass that extends the abstract class. Abstract Class with an Abstract MethodThe following example shows an abstract class that contains an abstract method. Subclasses must provide the implementation for the abstract method. Here, Bike is an abstract class with the abstract method run(). The subclass Honda provides the implementation of run(). This shows how abstract classes define a method structure while leaving the details to subclasses. ExampleCompile and RunOutput: running safely More Examples of Abstract ClassPractice the following examples to better understand the concept of abstract classes and how they are used in Java. Example 1: Shape Abstract ClassIn this example, Shape is an abstract class, and its implementation is provided by the Rectangle and Circle classes. The specific implementation is usually hidden from the end user and may be provided through a factory method. ExampleCompile and RunOutput: drawing circle Example 2: Bank Abstract ClassThis example shows how abstract classes can define a common interface for related classes, while subclasses provide their own implementation. ExampleCompile and RunOutput: Rate of Interest is: 7 % Rate of Interest is: 8 % Abstract Class having Constructor, Data Members, and MethodsAn abstract class in Java can contain data members, abstract methods, concrete methods, constructors, and even the main() method. ExampleThe following example shows an abstract class Bike with:
ExampleCompile and RunOutput:
bike is created
running safely..
gear changed
Rules of Abstract Classes1. If a class contains an abstract method, the class itself must be declared abstract. Output: compile time error 2. If a subclass extends an abstract class with abstract methods, it must either implement all abstract methods or be declared abstract itself. Output: Honda started Another Real Scenario of Abstract ClassThe abstract class can also be used to provide some implementation of the interface. In such case, the end user may not be forced to override all the methods of the interface. Note: If we are beginner to Java, learn interface first then see this example.ExampleCompile and RunOutput: I am a I am b I am c I am d Next TopicInterface in Java |
We request you to subscribe our newsletter for upcoming updates.