In this Java tutorial, you will learn about abstraction in Java, why it is used in object-oriented programming, and how to implement abstraction using interfaces, abstract methods, and abstract classes.

What Abstraction Means in Java OOP

Abstraction is one of the core concepts of Objected Oriented Programming. When there is a scenario that you are aware of what functionalities a class should have, but not aware of how the functionality is implemented or if the functionality could be implemented in several ways, it is advised to use abstraction. So, when some class is using(extending or implementing) this abstract class or methods, they may implement the abstract methods.

In simple words, abstraction in Java focuses on what an object can do, while hiding the internal details of how it does it. For example, a car can accelerate and brake. The user of the car does not need to know every internal mechanical or electronic detail behind these actions.

Abstraction in Java could be achieved with the following ways.

  • Interfaces
  • Abstract Methods and Classes
Abstraction in Java

Abstraction in Java Using Interfaces

Interfaces have similar structure as that of a class, but with abstract methods and variables that are static & final . Which means, the variables of interfaces are constants and methods do not have implementations. But an Interface in Java enforces definite implementations of methods/behaviors/routines in classes that implements the interface.

Following Car.java is an example of a Java Interface.

Car.java

</>
Copy
public interface Car {
	public void accelerate();
	public void breaking();
}

The interface above tells every implementing class that it must provide behavior for accelerate() and breaking(). The interface does not decide how acceleration or braking happens. That detail is left to the class that implements the interface.

ElectricCar.java

</>
Copy
public class ElectricCar implements Car {
    @Override
    public void accelerate() {
        System.out.println("Electric car accelerates using motor power.");
    }

    @Override
    public void breaking() {
        System.out.println("Electric car slows down using regenerative braking.");
    }
}

PetrolCar.java

</>
Copy
public class PetrolCar implements Car {
    @Override
    public void accelerate() {
        System.out.println("Petrol car accelerates using engine power.");
    }

    @Override
    public void breaking() {
        System.out.println("Petrol car slows down using hydraulic brakes.");
    }
}

Both classes follow the same abstraction, but each class gives its own implementation. This is useful when different classes must expose the same behavior with different internal logic.

Main.java

</>
Copy
public class Main {
    public static void main(String[] args) {
        Car car1 = new ElectricCar();
        Car car2 = new PetrolCar();

        car1.accelerate();
        car1.breaking();

        car2.accelerate();
        car2.breaking();
    }
}

Output

Electric car accelerates using motor power.
Electric car slows down using regenerative braking.
Petrol car accelerates using engine power.
Petrol car slows down using hydraulic brakes.

In real project code, the method name would usually be brake() or applyBrake(). The examples above keep breaking() to match the existing interface shown in this tutorial.

Abstraction in Java Using Abstract Methods and Abstract Classes

An abstract method has only declaration part but no implementation or definition is provided.

An abstract class is a class that is declared abstract. And abstract class can have none or one or more abstract methods. But if there is at least one abstract method in a class, the class has to be declared abstract. And an important point to be noted is that an abstract class can never be instantiated, i.e., an object of abstract class type can never be created.

An abstract class can only be a super class for other normal classes or other abstract classes.

In the following program, Vehicle is an abstract class with abstract methods accelerate() and breaking().

Vehicle.java

</>
Copy
public abstract class Vehicle {
	public abstract void accelerate();
	public abstract void breaking();
}

A concrete subclass must implement all inherited abstract methods, unless the subclass is also declared abstract.

Bike.java

</>
Copy
public class Bike extends Vehicle {
    @Override
    public void accelerate() {
        System.out.println("Bike accelerates by increasing throttle.");
    }

    @Override
    public void breaking() {
        System.out.println("Bike slows down using disc brakes.");
    }
}

Main.java

</>
Copy
public class Main {
    public static void main(String[] args) {
        Vehicle vehicle = new Bike();

        vehicle.accelerate();
        vehicle.breaking();
    }
}

Output

Bike accelerates by increasing throttle.
Bike slows down using disc brakes.

Java Abstract Class with Both Abstract and Concrete Methods

An abstract class is not limited to abstract methods. It can also contain fields, constructors, and concrete methods. This makes an abstract class useful when subclasses share some common code but still need to provide their own implementation for selected methods.

Payment.java

</>
Copy
public abstract class Payment {
    private double amount;

    public Payment(double amount) {
        this.amount = amount;
    }

    public void printAmount() {
        System.out.println("Amount: " + amount);
    }

    public abstract void processPayment();
}

CardPayment.java

</>
Copy
public class CardPayment extends Payment {
    public CardPayment(double amount) {
        super(amount);
    }

    @Override
    public void processPayment() {
        System.out.println("Processing card payment.");
    }
}

Main.java

</>
Copy
public class Main {
    public static void main(String[] args) {
        Payment payment = new CardPayment(2500.00);

        payment.printAmount();
        payment.processPayment();
    }
}

Output

Amount: 2500.0
Processing card payment.

Here, printAmount() is common behavior, while processPayment() is abstract behavior. Every payment type can reuse the common code and still define its own payment processing logic.

Abstract Class vs Interface in Java Abstraction

Both abstract classes and interfaces support abstraction in Java, but they are used for different design needs.

FeatureAbstract ClassInterface
Main purposeDefines a common base with shared code and required behaviorDefines a contract that implementing classes must follow
Keyword usedabstract with extendsinterface with implements
Object creationCannot be instantiated directlyCannot be instantiated directly
MethodsCan have abstract and concrete methodsCan declare abstract methods and can also have default/static methods in modern Java
FieldsCan have instance fieldsFields are public, static, and final by default
Inheritance modelA class can extend only one classA class can implement multiple interfaces
Best useUse when related classes share state or common implementationUse when different classes must follow the same behavior contract

Rules for Abstract Methods and Abstract Classes in Java

  • An abstract method has no method body.
  • A class that contains at least one abstract method must be declared as abstract.
  • An abstract class cannot be instantiated directly using new.
  • A concrete subclass must implement all inherited abstract methods.
  • An abstract class can contain constructors, fields, concrete methods, static methods, and final methods.
  • An abstract class can extend another abstract class or a normal class.
  • An interface is generally preferred when you want to define only a behavior contract.

Syntax of Abstract Class and Interface in Java

The following syntax examples show the basic structure of abstraction in Java.

Abstract class syntax

</>
Copy
abstract class ClassName {
    abstract returnType methodName();

    void concreteMethod() {
        // method body
    }
}

Interface syntax

</>
Copy
interface InterfaceName {
    void methodName();
}

Abstraction, Encapsulation, Inheritance, and Polymorphism in Java

Abstraction is one of the four major OOP concepts in Java. The other three are encapsulation, inheritance, and polymorphism. They are related, but they solve different design problems.

OOP conceptMeaning in Java
AbstractionHides implementation details and exposes essential behavior
EncapsulationWraps data and methods together, often using private fields and public methods
InheritanceAllows one class to reuse or extend another class
PolymorphismAllows the same method call to behave differently based on the object

For example, an interface gives abstraction by declaring required methods. A class that implements that interface can also use encapsulation by keeping its fields private. A subclass can use inheritance, and a parent type reference can show polymorphism at runtime.

When to Use Abstraction in Java Programs

  • Use abstraction when multiple classes must follow the same set of behaviors.
  • Use an interface when unrelated classes need to provide the same operations.
  • Use an abstract class when related classes need common fields or reusable method logic.
  • Use abstraction to reduce dependency on implementation details.
  • Use abstraction when you want code to work with a parent type, while actual behavior is supplied by child classes.

Common Mistakes While Using Abstraction in Java

  • Trying to create an object directly from an abstract class.
  • Declaring an abstract method inside a non-abstract class.
  • Forgetting to implement all abstract methods in a concrete subclass.
  • Using an abstract class where a simple interface would be clearer.
  • Adding too many unrelated methods to an interface, making every implementing class depend on behavior it does not need.

Abstraction in Java FAQs

What is abstraction in Java?

Abstraction in Java is an object-oriented programming concept where essential behavior is exposed while implementation details are hidden. It is implemented mainly through abstract classes and interfaces.

How is abstraction achieved in Java?

Abstraction is achieved in Java using interfaces, abstract methods, and abstract classes. Interfaces define behavior contracts, while abstract classes can define both common implementation and abstract behavior.

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

No. An abstract class cannot be instantiated directly. However, a reference of the abstract class type can point to an object of a concrete subclass.

What is the difference between abstraction and encapsulation in Java?

Abstraction hides implementation details and exposes only essential behavior. Encapsulation hides data by keeping fields private and providing controlled access through methods such as getters and setters.

Should I use an abstract class or an interface for Java abstraction?

Use an interface when you need a behavior contract that many different classes can implement. Use an abstract class when related classes need shared state, constructors, or common method implementations.

Editorial QA Checklist for This Java Abstraction Tutorial

  • Confirm that the examples clearly separate method declaration from method implementation.
  • Check that every concrete subclass implements all inherited abstract methods.
  • Verify that interface examples use implements and abstract class examples use extends.
  • Ensure output blocks contain only program output and use the output class.
  • Review whether the tutorial explains when to use an interface and when to use an abstract class.

Summary of Abstraction in Java

In this Java Tutorial, we have learnt how to implement Abstraction in Java. Abstraction helps define what a class or object should do without exposing every implementation detail. In Java, interfaces are useful for defining behavior contracts, while abstract classes are useful when subclasses need a common base with shared code and required methods.