DEV Community

Cover image for What is Abstraction in Java and How is it Achieved?
Aswin Arya
Aswin Arya

Posted on

What is Abstraction in Java and How is it Achieved?

Abstraction is one of the fundamental concepts of Object-Oriented Programming (OOP) in Java. It helps developers simplify complex systems by showing only essential features while hiding internal implementation details.

Abstraction is widely used in real-world Java applications and is a very common Java interview question.

 What is Abstraction?

Abstraction means hiding implementation details and showing only the functionality to the user.

πŸ‘‰ In simple words:

The user knows what an object does, but not how it does it.

Real-World Example

Consider a car:

You use the steering, accelerator, and brakes.

You don’t need to know how the engine internally works.

This is abstraction β€” exposing only necessary functionality.

How is Abstraction Achieved in Java?

Abstraction is achieved using:

Abstract Classes

Interfaces

1️⃣ Abstract Class

An abstract class is a class declared using the abstract keyword.
It can contain:

Abstract methods (without body)

Normal methods (with implementation)

Example
abstract class Animal {

abstract void sound(); // abstract method

void eat() {
    System.out.println("Animal eats food");
}
Enter fullscreen mode Exit fullscreen mode

}

class Dog extends Animal {

void sound() {
    System.out.println("Dog barks");
}
Enter fullscreen mode Exit fullscreen mode

}

public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
a.eat();
}
}

Explanation

Animal defines behavior but hides implementation.

Dog provides actual implementation.

User interacts only with exposed functionality.

2️⃣ Interface

An interface provides 100% abstraction (conceptually).

It contains method declarations, and implementing classes define behavior.

Example
interface Vehicle {
void start();
}

class Car implements Vehicle {
public void start() {
System.out.println("Car starts with key");
}
}

Advantages of Abstraction

βœ” Reduces complexity
βœ” Improves code security
βœ” Enhances maintainability
βœ” Promotes loose coupling
βœ” Makes applications scalable

Abstraction vs Encapsulation (Quick Idea)

Abstraction β†’ Hides implementation details

Encapsulation β†’ Hides data

Interview Tip

A simple definition:

Abstraction is the process of hiding implementation details and exposing only essential functionality using abstract classes and interfaces.

****Conclusion

Abstraction helps developers focus on what an object should do rather than how it works internally. It is a key principle behind modern Java frameworks like Spring and enterprise applications.

πŸš€ No 1 Java Real Time Projects Online Training in Hyderabad β€” ashok it

Want to master Java concepts with real-time project experience?

Join our industry-oriented Java Full Stack training program designed by real time experts.

βœ” Core Java & Advanced Java
βœ” Spring Boot & REST API Development
βœ” Microservices Architecture
βœ” Real-Time Industry Projects
βœ” Interview Preparation & Placement Assistance

Build strong fundamentals and become a job-ready Java Full Stack Developer.

Top comments (0)