Back to Blog
Java Programming

Abstract Class in Java: Purpose and Example Code

Learn what an abstract class in Java is, why it is used, its syntax, abstract methods, inheritance, and practical Java code examples.

Published 18 November 2026

Abstract Class in Java: Purpose and Example Code

An abstract class in Java is an important concept in object-oriented programming (OOP). It helps developers create a common structure for related classes. It also allows developers to reuse code and organize applications more effectively.

In Java, an abstract class can contain both abstract methods and concrete methods. This means it can define methods without implementation and also provide methods with complete code.

For example, consider a payment system. Different payment methods, such as UPI, credit cards, and debit cards, follow a common payment process. However, each method handles payments differently. An abstract class can define the common structure, while individual classes provide their own implementations.

In this guide, you will learn what an abstract class in Java is, why developers use it, how to create one, and how it works with inheritance. You will also explore practical Java abstract class examples and understand the difference between an abstract class and an interface.

What Is an Abstract Class in Java?

An abstract class in Java is a class declared using the abstract keyword. It cannot be used to create objects directly. Instead, developers use it as a base class for other classes.

An abstract class can contain:

  • Abstract methods without a method body.
  • Concrete methods with complete implementation.
  • Constructors.
  • Variables and constants.
  • Static methods.
  • Final methods.

The main purpose of an abstract class is to provide a common structure for related classes while allowing them to implement specific features.

Simple Example

abstract class Animal {

abstract void sound();

void eat() {

System.out.println(“Animal is eating”);

}

}

In this example:

  • Animal is an abstract class.
  • sound() is an abstract method.
  • eat() is a concrete method.
  • The abstract method does not have a method body.

A child class must implement the abstract method unless the child class is also declared abstract.

Why Do We Use Abstract Classes in Java?

Developers use abstract classes to create reusable and organized code. They are useful when multiple classes share common features but need different implementations for certain methods.

Here are the main reasons to use abstract classes in Java.

1. Achieve Abstraction

Abstraction hides internal implementation details and shows only essential features. An abstract class helps developers define what a class should do without specifying every implementation detail.

2. Support Code Reusability

An abstract class can contain common methods. Child classes can inherit and reuse these methods without writing the same code again.

3. Maintain a Common Structure

Related classes can follow the same structure. This makes applications easier to manage and maintain.

4. Support Method Overriding

Child classes can provide their own implementation of abstract methods. This allows different classes to perform similar tasks in different ways.

5. Improve Application Design

Abstract classes help developers organize large applications. They separate common functionality from class-specific functionality.

Abstract Class Syntax in Java

Creating an abstract class is simple. You need to use the abstract keyword before the class declaration.

Syntax

abstract class ClassName {

abstract void methodName();

void display() {

System.out.println(“Concrete method”);

}

}

Syntax explanation:

  • abstract: Declares the class as abstract.
  • ClassName: Represents the name of the class.
  • abstract void methodName(): Declares a method without implementation.
  • display(): Represents a concrete method with implementation.

An abstract class does not need to contain an abstract method. You can declare a class abstract even if all its methods have complete implementations.

Java Abstract Class Example with Code

Let’s understand abstract class implementation in Java with a simple example.

Consider a vehicle application. Cars and bikes are different types of vehicles. Both have a common method called start(), but each vehicle may start differently.

Step 1: Create an Abstract Class

abstract class Vehicle {

abstract void start();

void fuel() {

System.out.println(“Vehicle needs fuel or energy”);

}

}

Here, Vehicle is an abstract class. It contains one abstract method and one concrete method.

Step 2: Create a Child Class

class Car extends Vehicle {

void start() {

System.out.println(“Car starts with a key”);

}

}

The Car class extends the Vehicle class and provides an implementation for the start() method.

Step 3: Create Another Child Class

class Bike extends Vehicle {

void start() {

System.out.println(“Bike starts with a button”);

}

}

The Bike class also inherits the Vehicle class. However, it provides a different implementation of the start() method.

Step 4: Execute the Program

public class Main {

public static void main(String[] args) {

Vehicle car = new Car();

Vehicle bike = new Bike();

car.start();

car.fuel();

bike.start();

bike.fuel();

}

}

Output

Car starts with a key

Vehicle needs fuel or energy

Bike starts with a button

Vehicle needs fuel or energy

What does this example teach us?

  • An abstract class provides a common structure.
  • Child classes inherit common methods.
  • Each child class implements abstract methods differently.
  • Developers can reuse code and avoid duplication.

This is a simple example of inheritance and abstraction in Java.

What Are Abstract Methods in Java?

An abstract method is a method declared without a body. It defines what a child class must do, but it does not explain how to perform the task.

Abstract methods are declared using the abstract keyword.

Example

abstract class Employee {

abstract void calculateSalary();

}

In this example, calculateSalary() is an abstract method. It does not contain any implementation.

A child class can implement this method based on its requirements.

class Developer extends Employee {

void calculateSalary() {

System.out.println(“Developer salary calculated”);

}

}

The Developer class provides the implementation of the abstract method.

Difference Between Abstract and Concrete Methods

Abstract and concrete methods are two important parts of Java abstraction.

Abstract MethodConcrete Method
Does not have a method bodyHas a method body
Uses the abstract keywordDoes not require the abstract keyword
Must be implemented by a concrete child classCan be inherited directly
Defines what a method should doDefines how a method works

Example

abstract class Shape {

abstract void draw();

void display() {

System.out.println(“Displaying shape”);

}

}

Here, draw() is an abstract method, while display() is a concrete method.

Abstract Class vs Concrete Class in Java

An abstract class and a concrete class serve different purposes in Java.

Abstract ClassConcrete Class
Declared using the abstract keywordDoes not require the abstract keyword
Cannot be instantiated directlyCan be instantiated
Can contain abstract methodsCannot contain abstract methods
Can contain concrete methodsContains implemented methods
Used as a base classUsed to create objects

For example, Vehicle can be an abstract class, while Car can be a concrete class.

abstract class Vehicle {

abstract void start();

}

class Car extends Vehicle {

void start() {

System.out.println(“Car started”);

}

}

Here, you can create a Car object but cannot directly create a Vehicle object.

Abstract Class vs Interface in Java

Both abstract classes and interfaces support abstraction in Java. However, they have different features and use cases.

Abstract ClassInterface
Declared using abstract classDeclared using interface
A class can extend only one classA class can implement multiple interfaces
Can have instance variablesFields are implicitly public, static, and final
Can have constructorsCannot have constructors
Can contain abstract and concrete methodsCan contain abstract, default, static, and private methods
Supports shared state and implementationDefines a contract that classes follow

When Should You Use an Abstract Class?

Use an abstract class when related classes share common variables, constructors, and methods.

For example, Car, Bike, and Bus can inherit common vehicle properties.

When Should You Use an Interface?

Use an interface when different classes need to follow the same contract, even if they are not related through inheritance.

For example, Car, Printer, and PaymentService could implement a common Startable or Service interface based on their requirements.

Can We Create an Object of an Abstract Class in Java?

No. You cannot create an object directly from an abstract class.

For example:

abstract class Animal {

abstract void sound();

}

public class Main {

public static void main(String[] args) {

Animal a = new Animal(); // Compilation error

}

}

This code produces a compilation error because Java does not allow direct object creation from an abstract class.

However, you can create a reference variable of an abstract class and assign a child class object to it.

Animal a = new Dog();

This is valid when Dog extends Animal and implements its abstract methods.

Can an Abstract Class Have Constructors in Java?

Yes. An abstract class can have constructors.

Although you cannot create an abstract class object directly, its constructor runs when a child class object is created.

Example

abstract class Animal {

Animal() {

System.out.println(“Animal constructor called”);

}

abstract void sound();

}

class Dog extends Animal {

void sound() {

System.out.println(“Dog barks”);

}

}

public class Main {

public static void main(String[] args) {

Dog d = new Dog();

d.sound();

}

}

Output:

Animal constructor called

Dog barks

The parent class constructor executes before the child class constructor body. This helps initialize common properties.

Java Abstraction with Real-Time Examples

Java abstraction is widely used in real-world software applications. It helps developers create flexible and maintainable systems.

Here are some practical examples.

1. Banking Application

A banking application may support savings accounts, current accounts, and fixed deposits.

An abstract Account class can define common methods such as deposit() and withdraw(). Individual account classes can implement their specific rules.

2. Payment Application

Payment applications support UPI, credit cards, and debit cards.

An abstract Payment class can define a common processPayment() method. Each payment type can provide its own implementation.

3. Vehicle Management System

A vehicle management application may include cars, bikes, buses, and trucks.

An abstract Vehicle class can define common methods such as start() and stop(). Each vehicle can implement these methods differently.

4. Employee Management System

An employee management system may include developers, managers, and testers.

An abstract Employee class can define common employee details and salary-related methods. Each employee type can calculate its salary differently.

These examples show how abstraction helps organize Java applications.

Common Rules of Abstract Classes in Java

Before using abstract classes, remember these important rules:

  1. An abstract class must be declared using the abstract keyword.
  2. You cannot create an object directly from an abstract class.
  3. An abstract class can have abstract and concrete methods.
  4. An abstract class can contain constructors.
  5. An abstract class can contain variables and static methods.
  6. A concrete child class must implement all inherited abstract methods.
  7. An abstract class can extend another class and implement interfaces.
  8. An abstract method cannot be declared as private, static, or final.
  9. An abstract class can exist without any abstract methods.
  10. A child class that does not implement all abstract methods must also be declared abstract.

Frequently Asked Questions (FAQs)

1. What is an abstract class in Java with an example?

An abstract class is a class declared using the abstract keyword. It cannot be instantiated directly. For example, an abstract Vehicle class can define a start() method that Car and Bike implement differently.

2. Why do we use abstract classes in Java?

Developers use abstract classes to achieve abstraction, reuse common code, support inheritance, and create a common structure for related classes.

3. How do you create an abstract class in Java?

Use the abstract keyword before the class declaration.

abstract class Animal {

abstract void sound();

}

4. What is the purpose of abstraction in Java?

The purpose of abstraction is to hide unnecessary implementation details and show only essential functionality. It makes applications easier to develop and maintain.

5. What is the difference between an abstract class and an interface?

An abstract class can contain constructors, instance variables, and concrete methods. An interface defines a contract and supports multiple interface implementation by a class.

6. Can we create an object of an abstract class in Java?

No. Java does not allow direct object creation from an abstract class. However, you can create an object of its concrete child class.

7. Can an abstract class have constructors in Java?

Yes. An abstract class can have constructors. They are called when an object of its child class is created.

8. Can an abstract class have normal methods?

Yes. An abstract class can contain concrete methods with complete implementations. Child classes can use or override these methods.

9. How does inheritance work with abstract classes in Java?

A child class uses the extends keyword to inherit an abstract class. It must implement all inherited abstract methods unless the child class is also abstract.

10. Is abstraction possible without an abstract class?

Yes. Java also supports abstraction through interfaces. Developers can use interfaces to define common contracts for different classes.

Conclusion

An abstract class in Java is an important concept in object-oriented programming. It helps developers achieve abstraction, reuse code, and create a common structure for related classes.

By understanding abstract methods, concrete methods, inheritance, and the difference between abstract classes and interfaces, you can build better Java applications.

If you want to improve your Java programming skills, learning these concepts with practical coding examples is essential.

At XpertsEdge Technologies, learners can strengthen their programming knowledge through practical Core Java training, Java OOP concepts, and real-time project development. Whether you are a beginner or preparing for Java developer interviews, understanding abstraction is an important step towards becoming a skilled Java developer.