Abstraction in JavaLast Updated : 8 Jan 2026 In Java, abstraction is one of the important concepts of object-oriented programming system. Abstraction is used to hide implementation details and showing only essential features to the user. In this chapter, we will learn about abstraction, how to achieve it with different approaches. What is Abstraction in Java?features or capabilities that are necessary to the user. It emphasizes what an object does rather than how it does it. The following are the important features of abstraction in Java:
Understanding Abstraction with Real-World ExamplesThe following real-world examples help explain this concept clearly:
Achieving Abstraction in JavaIn Java, abstraction can be achieved in the following two ways:
Let's understand the both approaches to achieve abstraction in details. 1. Abstraction Using Abstract ClassAbstraction can be achieved using an abstract class that may contain abstract methods (without a body) as well as concrete methods (with implementation). The abstract methods define what needs to be done, while subclasses provide how it should be done by implementing those methods. SyntaxThe following syntax shows how abstraction is implemented in Java using an abstract class with abstract and non-abstract methods: Example of Abstraction Using Abstract ClassThe following example demonstrates abstraction using an abstract class, where the Animal class defines an abstract method and a concrete method, and the Dog class provides the implementation of the abstract method. Output: Bark bark I can eat. 2. Abstraction Using InterfaceIn Java, another way to implement abstraction is by using interfaces. Interfaces are useful for achieving 100% abstraction. In Java, as well as in other languages, interfaces include both variables and methods, but do not provide a method body. Therefore, subclasses must implement every method of the interface. Multiple inheritance is also possible through interfaces. Note that the methods of an interface are public and abstract by default. SyntaxIn the following code snippet, the Bike class implements the Drivable interface and also includes the drive() method. Example of Abstraction Using InterfaceThe following example demonstrates abstraction using an interface, where the Person interface declares a method and different classes (Student and Lecturer) provide their own implementations of that method. Output: This is the display method of the student class This is the display method of the lecturer class Advantages of AbstractionThe following list shows the advantages of abstraction in Java.
Disadvantages of AbstractionThe following list shows the disadvantages of abstraction in Java.
Common Mistakes to AvoidThe following is a list of common errors that can occur while working with abstraction, which should be avoided:
Next TopicREPL in Java |
We request you to subscribe our newsletter for upcoming updates.