Understanding Abstraction in Java with Example: A Simple Guide

Robert Jul 13, 2026

In the realm of object-oriented programming, particularly in Java, abstraction is a crucial concept that aids in creating manageable and reusable code. It enables us to focus on what an object does instead of how it does it, thereby enhancing the readability and simplicity of our code. Let's delve into this concept, complete with practical examples.

What is An Abstraction in Java? Abstract Method and Examples
What is An Abstraction in Java? Abstract Method and Examples

Abstraction in Java is primarily achieved through abstract classes and interfaces. It serves two primary purposes: reducing code complexity and providing a common interface for diverse implementations.

Know Abstraction in Java also Explore Abstraction vs Encapsulation in Java
Know Abstraction in Java also Explore Abstraction vs Encapsulation in Java

Understanding Abstract Classes in Java

An abstract class in Java is a class that cannot be instantiated but can be inherited. It can contain both abstract methods (methods without a body) and non-abstract methods.

Java Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually

Here's a simple example. Let's say we want to create a shape hierarchy. We could have an abstract class 'Shape':

public abstract class Shape {
    public abstract void calculateArea();
}

And concrete classes like 'Circle' and 'Rectangle' could extend it:

Abstraction in Java - Learn with its Types and Real-life Examples - TechVidvan
Abstraction in Java - Learn with its Types and Real-life Examples - TechVidvan

public class Circle extends Shape {
    @Override
    public void calculateArea() {
        // implementation here
    }
}

Concrete Methods in Abstract Classes

Abstract classes can also contain concrete methods, providing default behavior for subclasses to inherit.

For instance, in our 'Shape' example, we could have a non-abstract method 'displayDetails()' which all shapes will inherit:

What is abstraction in java?
What is abstraction in java?

public class Shape {
    public abstract void calculateArea();
    public void displayDetails() {
        // common implementation here
    }
}

Final and Private Methods in Abstract Classes

From Java 8 onwards, abstract classes can also have final and private methods, which cannot be overridden by subclasses.

If we don't want a method to be overridden, we can declare it as 'final' or 'private'. For example, in our 'Shape' class, we might have a final method 'getColor()', knowing that all shapes should have a color but we don't want each shape to override this behavior:

an image of a man opening a atm machine with the words,'how do you use it? '
an image of a man opening a atm machine with the words,'how do you use it? '

public class Shape {
    public final String getColor() {
        return "White";
    }
    // other methods here
}

Java Interfaces: Another Way to Achieve Abstraction

Interfaces in Java are a way of achieving abstraction by providing a contract that implementing classes must follow.

Abstraction in Java | Abstract Class vs Interface Explained Simply
Abstraction in Java | Abstract Class vs Interface Explained Simply
What is Abstract Class and Method in Java? Examples and Frequently asked Questions
What is Abstract Class and Method in Java? Examples and Frequently asked Questions
Abstraction in Java Explained with Easy Example 🚀 | Java OOPs Concepts
Abstraction in Java Explained with Easy Example 🚀 | Java OOPs Concepts
30 Days of Java – Day 16 | Java Abstraction Explained for Beginners
30 Days of Java – Day 16 | Java Abstraction Explained for Beginners
Abstraction in Java 🎭
Abstraction in Java 🎭
Difference between Abstraction and Encapsulation
Difference between Abstraction and Encapsulation
Java Variables Explained | Local, Static & Instance Variables in Java
Java Variables Explained | Local, Static & Instance Variables in Java
Abstract Class Vs Interface in Java
Abstract Class Vs Interface in Java
Abstract Class vs Interface - What really differenciates them?
Abstract Class vs Interface - What really differenciates them?
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
Java
Java
an image of people working in the office
an image of people working in the office
🔥 Java Abstraction Explained! 🚀
🔥 Java Abstraction Explained! 🚀
a pink and orange poster with words on it that say,'what is the meaning?
a pink and orange poster with words on it that say,'what is the meaning?
Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
30 Days of Java – Day 17 | Java Interfaces Explained for Beginners
30 Days of Java – Day 17 | Java Interfaces Explained for Beginners
Java OOPs Concept-Abstraction
Java OOPs Concept-Abstraction
Java Polymorphism - Master the Concept with Real-life Examples! - TechVidvan
Java Polymorphism - Master the Concept with Real-life Examples! - TechVidvan
Java if Statement 🚦 | Make Smart Decisions with Code!
Java if Statement 🚦 | Make Smart Decisions with Code!
an image of different types of logos in the language of webpages and apps
an image of different types of logos in the language of webpages and apps

Let's extend our 'Shape' example to include an interface 'Printable':

public interface Printable {
    void printShapeDetails();
}

Any class that implements 'Printable' must provide an implementation for 'printShapeDetails()'. For instance:

public class Circle extends Shape implements Printable {
    @Override
    public void calculateArea() {
        // implementation here
    }
    @Override
    public void printShapeDetails() {
        // implementation here
    }
}

Multiple Inheritance through Interfaces

In Java, a class can implement multiple interfaces, enabling a form of multiple inheritance for the 'is-a' relationship.

For example, a 'ShinyCircle' could implement 'Printable' and another interface 'Shiny':

public interface Shiny {
    String getShineLevel();
}

public class ShinyCircle extends Circle implements Printable, Shiny {
    // implementations here
}

Default Methods in Interfaces

From Java 8 onwards, interfaces can have default methods, providing a default behavior which implementing classes can inherit or override.

For instance, we could add a default method 'display()' to our 'Printable' interface:

public interface Printable {
    default void display() {
        System.out.println("Displaying shape details..");
    }
    void printShapeDetails();
}

Lastly, abstraction in Java is a powerful tool that helps in creating robust, modular, and reusable code. It encourages code reusability and promotes the single responsibility principle, making your codebase more maintainable and testable. Always strive to find the right level of abstraction for your objects, ensuring they are neither too concrete nor too abstract.