Abstraction in Object Orient Programming is one of the main concepts of Object-Oriented Programming (OOP). Abstraction in Object Orient Programming shows the required information to the outside and hides the lower-level complex implementation.
Let’s try to understand what is abstraction in Java, examples of abstraction in Java, advantages of abstraction in Java, and abstraction vs encapsulation in Java in the next sections.
It is hiding the complexity of implementation. Therefore able to design things on top of abstract information but do not want to worry about implementation. So abstraction is a better design-level concept for planning the program without thinking about the complexity of the implementation.
Abstraction Real-life Example
Example 1

Let’s take your car as the first example from the real world. When it considers car design, it gives you a Key(Switch) to start the car, a steering wheel for turning the car and Brake paddles, accelerator paddles to speed up and slow down the car, etc…
This means car users do not need to know about how works engine, brakes, steering wheel, and their complexity. Once design the car Just only need to think about what a car looks like, what type of features includes, and which plan is used to build it.
Example 2

Now take another example of Visa/Master Online Payment. The payment gateway needs information like card number, CVV, Card Holder Name, and expiration date to proceed with payments.
In this case, a debit/credit card is created with basic information but don’t want to know how it captures the amount from their account. However there is a complex process of validating the input information, pre-auth, validating the account from an account-created bank, capturing the amount if fails refund or rollback, etc. But that is not essential on a design level. Just make it. Therefore it hides the complex implementation.
Abstraction Real-life Example with Java
Achieving abstraction in java by using Interface or Abstract Class. The interface is 100% abstract but Abstract Class can have non-abstract methods. Abstract methods are known as without body (Only method initialization with Empty body) methods.
// Here you can see that car is abstract model. // When design level no need to worry about how implement the Accelerate. // just go with essential attributes and behaviors. abstract class Car { protected String color ; protected double speed ; public Car(String color) { this.color=color; } abstract void accelerate(); public String getColor(){ System.out.println("Return the Color :"+ color); return color; } public double getSpeed(){ System.out.println("Return the Speed :"+ speed); return speed; } }
// Now Implement abstract model to real world BMW car class Bmw extends Car{ public Bmw(String color) { super(color); } // Implement the complex Accelerate mechanism for BMW public void accelerate(){ speed = (speed+50); System.out.println("Speed up the BMW car "+speed); } public String toString(){ return "BMW color is " + getColor()+ "and speed is : " + getSpeed(); } }
// Now Implement abstract model to real world CIVIC car class Civic extends Car{ public Civic(String color) { super(color); } // Implement the complex Accelerate mechanism for CIVIC public void accelerate(){ speed = (speed+100); System.out.println("Speed up the CIVIC car "+speed); } public String toString(){ return "CIVIC color is " + getColor()+ "and speed is : " + getSpeed(); } }
public class TestAbstract { public static void main(String[] args) { Car myBmw =new Bmw("Red"); myBmw.accelerate(); myBmw.accelerate(); System.out.println("My Car "+myBmw); Car myCivic =new Civic("Blue"); myCivic.accelerate(); myCivic.accelerate(); System.out.println("My Car "+myCivic); } }
Output
Speed up the BMW car 50.0 Speed up the BMW car 100.0 Return the Color :Red Return the Speed :100.0 My Car BMW color is Redand speed is : 100.0 Speed up the CIVIC car 100.0 Speed up the CIVIC car 200.0 Return the Color :Blue Return the Speed :200.0 My Car CIVIC color is Blueand speed is : 200.0
Abstraction Vs Encapsulation
Abstraction | Encapsulation |
Encapsulation is an implementation-level programming concept | Encapsulation is everything combining and keeping together as a single unit |
Abstraction is Design level programming concept | Abstract classes or interfaces used to achieve abstraction |
Abstract class or interfaces used to achieve abstraction | Access modifiers used to achieve encapsulation |
Data hiding is part of the abstraction | Data hiding is part of the encapsulation |
Code complexity hides using abstract classes and interfaces | Use getter and setter to hide data |
What are the advantages of Abstraction in Object Orient Programming?
- Improve the security. Because of giving the required information to outside
- Code Reusability. Abstracted information derived through the inheritance
- Reduce the code complexity by hiding lower level implementation
- Clear and Maintainability.
- Easy to maintain when it’s had a good design
- Easy to change when it’s clear and well-planned
- High-level design is used as a base element and if it changes those will apply to all.
Summary
Abstraction in Object Orient Programming is the most used OOP concept in the design phase. It gives the above advantages to the program once it is used. Therefore abstraction is a good design-level concept to plan the program without thinking about the complexity of the application.