Abstraction in Java: Types of Abstraction Explained Examples
Updated on May 27, 2025 | 9 min read | 7.5K+ views
Share:
For working professionals
For fresh graduates
More
Updated on May 27, 2025 | 9 min read | 7.5K+ views
Share:
Table of Contents
Did you know that Java 24, released in March 2025, boosts performance with Virtual Threads Stabilization, letting you manage millions of threads effortlessly? Plus, Flexible Constructor Bodies (JEP 492) offer more expressiveness, and Stream Gatherers (JEP 485) take data processing to the next level! Exciting advancements for Java developers! |
Abstraction in Java is a core concept that allows developers to hide the complex implementation details and expose only the essential functionality. It simplifies code and makes it more maintainable by focusing on what an object does rather than how it does it. This is achieved through abstract classes and interfaces in Java, which help define the structure of objects while leaving the details to be handled later.
In this blog, we’ll cover types of abstraction in Java like abstract class vs interface java, showcase practical examples, and highlight how abstraction improves code efficiency.
Abstraction is one of the core principles of object-oriented programming (OOP), and it simplifies complex systems by hiding unnecessary details and exposing only essential functionality to the user. In Java, abstraction is achieved primarily through abstract classes and interfaces, allowing developers to focus on what an object does, rather than how it does it.
The industry seeks professionals who are proficient in Java and its core concepts like abstraction. To gain the skills needed to excel, explore these top courses and level up your Java expertise.
An abstract class is a class declared with the abstract keyword. This class may contain abstract methods, which are methods without an implementation. These abstract methods must be implemented by subclasses. An abstract class can also contain non-abstract methods, which can provide default behavior. This flexibility allows abstract classes to provide a foundation for more specific implementations, ensuring that the essential structure remains intact while enabling customization.
Abstraction in Java helps to separate the interface (what an object does) from the implementation (how it does it), providing a higher level of control over how objects interact in a program.
For example, when defining a general Shape class, it may have an abstract method calculateArea(). Different subclasses, such as Circle or Rectangle, can then provide their own specific implementations of how to calculate the area, but the structure remains consistent.
Also Read: Why is Java Platform Independent Language?
There are two primary types of abstraction in Java:
Also Read: Abstract Class vs Interface: The Differences and the Similarities
Having a clear understanding of abstraction in Java, let’s explore the benefits of abstraction in Java.
The use of data abstraction in Java offers several benefits.
Simplicity: By hiding complex implementation details, abstraction allows developers to work with high-level interfaces, reducing the cognitive load when interacting with complex systems.
Code Reusability: Abstraction allows the reuse of code through inheritance. Abstract classes and interfaces provide a foundation for creating new classes without having to rewrite shared functionality.
Maintainability and Extensibility: When changes are made to the implementation in abstract classes or interfaces, the underlying details can be modified without affecting other parts of the system that depend on the abstraction. This separation ensures that the system can be more easily extended and maintained.
Flexibility: Through abstraction, Java allows for the implementation of different behaviors for similar operations. A good example is the Shape class mentioned earlier, where different shapes (like circles, rectangles, etc.) can have their own ways to calculate the area but adhere to the same interface.
Also Read: Types of Inheritance in Java: Single, Multiple, Multilevel & Hybrid
Common Uses of Data Abstraction in Java
Data abstraction in Java is a powerful tool for simplifying complex systems and enhancing code reusability, maintainability, and scalability. Below are some common use cases where data abstraction plays a key role in Java programming:
1. Implementation of Libraries and Frameworks
In Java, libraries and frameworks often use abstract classes and interfaces to define a set of operations while hiding the implementation details. This ensures that developers can interact with the library without needing to understand the internal workings.
Abstraction Example Java:
A Database Access Library might define an interface DatabaseOperations with methods like connect(), disconnect(), and query(). Different database implementations, such as MySQL or MongoDB, can implement this interface while keeping their specific implementations hidden. Developers can use the library without worrying about the details of how each database works.
2. Designing User Interfaces (UI)
UI frameworks in Java, such as Swing and JavaFX, heavily rely on abstraction to provide a flexible and standardized way of designing user interfaces. Abstract classes and interfaces define the structure for common UI components, and developers can extend or implement these to create customized components.
Abstraction Example Java:
In Swing, the JComponent class is an abstract class that defines methods like paintComponent() for rendering UI elements. Developers can extend this class and override the methods to create custom UI components like buttons, sliders, or text fields.
3. Abstracting External API Interactions
Data abstraction is also used when interacting with third-party APIs or external services. By creating an abstraction layer, developers can simplify the communication with external systems, ensuring that the application remains decoupled from specific API implementations.
Abstraction Example Java:
A payment gateway integration might define an interface PaymentProcessor with methods like processPayment() and refund(). Different payment providers, such as Stripe or PayPal, can implement this interface, hiding their specific API calls behind a standard interface.
4. Creating Extensible and Modular Systems
Abstraction allows for the creation of extensible and modular systems. By defining common behaviors in abstract classes and interfaces, developers ensure that the system can grow without requiring modifications to the core logic.
Abstraction Example Java:
A plugin-based application might define an interface Plugin with methods like initialize() and execute(). Different plugins can implement this interface, adding their unique functionalities while maintaining compatibility with the core system.
5. Data Access Layers and Persistence Frameworks
Java Persistence APIs like JPA (Java Persistence API) or Hibernate use abstraction to manage interactions with databases. By abstracting SQL queries and database-specific operations, these frameworks allow developers to interact with high-level entities instead of dealing with raw database management.
Abstraction Example Java:
The EntityManager in JPA abstracts database operations such as persist(), find(), and remove(). This allows developers to interact with Java objects without writing complex SQL queries.
Also Read: Polymorphism in Java: Concepts, Types, Characteristics & Examples
Example of Data Abstraction in Java:
To explain the concept of data abstraction, let’s consider an example of a shape hierarchy. We can define an abstract class called Shape, which declares an abstract method calculateArea(). This class serves as a blueprint for different shapes like Circle and Rectangle that inherit from it. Each concrete shape class must provide its implementation of the calculateArea() method.
abstract class Shape {
public abstract double calculateArea();
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double calculateArea() {
return length * width;
}
}
public class Main {
public static void main(String[] args) {
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(4, 6);
System.out.println("Circle area: " + circle.calculateArea());
System.out.println("Rectangle area: " + rectangle.calculateArea());
}
}
Output:
Circle area: 78.53981633974483
Rectangle area: 24.0
Also Read: 53+ Key Selenium Java Interview Questions to Advance Your Career in 2025
An abstract method declares an object as abstract, and the object is not implemented. Here are some examples of the abstract method.
In the example below, Tea is the abstract class with only one abstract method implemented by the Honda class.
abstract class Tea{
abstract void run();
}
class Honda4 extends Tea{
void run(){System.out.println("running safely");}
public static void main(String args[]){
Tea obj = new Honda4();
obj.run();
}
}
Outcome - running safely
Figure is the abstract class implemented by the Rectangle and Circle classes in the example below. Here we don’t see the implementation class since it is hidden from the user. So to obtain the object of the implementation class, we use a method called the factory method. The factory method is a method that provides the instance of the class.
abstract class Figure{
abstract void draw();
}
//In real scenario, implementation is provided by others i.e. unknown by end user
class Rectangle extends Figure{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Figure{
void draw(){System.out.println("drawing circle");}
}
//In real scenario, method is called by programmer or user
class TestAbstraction1{
public static void main(String args[]){
Figure s=new Circle1();//In a real scenario, object is provided through method, e.g., getFigure() method
s.draw();
}
}
Outcome - drawing circle
An abstract class has an abstract or non-abstract method, a data member, constructor and also main method. The example below explains this.
//Example of an abstract class that has abstract and non-abstract methods
abstract class
Tea{
Tea(){System.out.println(“tea is created”);}
abstract void run();
void changeGear()
{System.out.println(“gear changed”);}
}
//Creating a Child class which inherits Abstract class
class Honda extends Tea{
void run(){System.out.println("running safely..");}
}
//Creating a Test class which calls abstract and non-abstract methods
class TestAbstraction2{
public static void main(String args[]){
Tea obj = new Honda();
obj.run();
obj.changeGear();
}
}
Outcome –
tea is created
running safely
Gear changed
Also Read: Learn 50 Java Projects With Source Code (2025 Edition)
Abstraction is a key concept in software development, and its primary purpose is to simplify complex systems by hiding unnecessary implementation details and exposing only the essential features. By focusing on what an object does rather than how it does it, abstraction allows developers to build cleaner, more modular, and maintainable code.
Here are some important reasons for using abstraction:
Abstraction reduces the complexity of systems by hiding irrelevant details and focusing only on the essential functionality. This allows developers to work with higher-level concepts and interact with objects without needing to understand the inner workings of those objects. For example, when interacting with a complex system, like a payment gateway, abstraction allows you to simply call methods like processPayment(), while hiding the intricate details of how the payment is processed.
Abstraction allows developers to define common behaviors through interfaces and abstract classes, which can be reused across different classes. This promotes code reusability, as the same abstract method or interface can be implemented by different classes, reducing redundancy and encouraging modular design.
Abstraction enables modular programming by separating functionality into independent components. This makes it easier to modify and extend the system without impacting other parts of the code. For example, if you want to add a new feature, like integrating a new payment service, you can implement a new class that follows the existing abstraction structure without altering the entire system.
By hiding the implementation details, abstraction isolates changes within the abstract class or interface, ensuring that modifications to the implementation don’t affect the rest of the system. This makes the system more maintainable as developers can modify the underlying code without disrupting the overall structure.
Abstraction helps enforce a consistent structure across different parts of an application by defining a clear contract or blueprint for how objects should behave. Interfaces and abstract classes provide a standard set of methods that all implementing classes must adhere to, ensuring uniformity.
By abstracting away unnecessary details, developers can focus on the critical aspects of an application, allowing them to write cleaner, more efficient code. Abstraction helps identify and define the core functionality of a system, ensuring that the program only exposes what is required for the user or other systems to interact with it.
After exploring the key reasons to use abstraction, let’s move to how upGrad can help you master abstraction and other Java concepts.
Abstraction in Java is a fundamental concept that helps simplify complex systems by exposing only essential information to the user while hiding unnecessary implementation details. As explained throughout the blog, abstraction in Java is achieved through abstract classes and interfaces, making it a powerful tool for creating more modular, maintainable, and scalable code.
To further enhance your understanding of abstraction in OOP and Java along with other core OOP concepts, upGrad’s courses offer comprehensive learning paths tailored to both beginners and experienced developers.
Here are some additional courses to further support your advancement in Java development.
References:
https://www.oracle.com/in/news/announcement/oracle-releases-java-24-2025-03-18/
https://klizos.com/java-24-updates-2025-for-backend-developers/
900 articles published
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology s...
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources