What is Abstraction in Object-Oriented Programming? Great concept Of OOP

Abstraction in Object-Oriented Programming
Share on facebook
Share on twitter
Share on linkedin
Share on email
Share on whatsapp
Share on pinterest
Share on print

Abstraction in Object-Oriented Programming is one of the main concepts of Object-Oriented Programming (OOP). Abstraction in Object-Oriented Programming is showing the required information to the outside and hides the lower-level complex implementation. Let’s try to understand what is abstraction in java, example 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

Simple Class Diagram for Car Abstract class

Let’s take your car as the first example from the real world. When it considers car design, it’s giving 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 no 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

Encapsulation
Abstraction use of Credit Cards Payments

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 cards create with basic information but don’t want to know how it captures the amount from their account. But 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
The concept of the abstraction is showing the required information to the outside and hiding the lower level complex implementationEncapsulation is everything combining and keeping together as a single unit
Abstraction is Design level programming conceptEncapsulation is an implementation level programming concept
Abstract class or interfaces used to achieve abstractionAccess modifiers used to achieve encapsulation
Data hiding is part of the abstractionData hiding is part of the encapsulation
Code complexity hides using abstract classes and interfacesUse getter and setter to hide data
Key Identifiers on Abstraction and Encapsulation

What are the advantages of OOP abstraction in 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 by 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 is the most used OOP concept in the design phase. It’s giving 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.

Share on facebook
Share on twitter
Share on linkedin
Share on email
Share on whatsapp
Share on pinterest
Share on print

24 thoughts on “What is Abstraction in Object-Oriented Programming? Great concept Of OOP”

  1. I was recommended this blog by means of my cousin. I am not positive whether this post is written by way of him as nobody else recognize such specified approximately my difficulty. You’re incredible! Thank you!

  2. I am not sure where you’re getting your info, but great topic. I needs to spend some time learning much more or understanding more.
    Thanks for great info I was looking for this information for my mission.

  3. Hi there! I could have sworn I’ve visited this web site before but after going through many of the posts I realized it’s new to me. Regardless, I’m definitely delighted I stumbled upon it and I’ll be bookmarking it and checking back often!

  4. Hi! I could have sworn I’ve visited this web site before but after looking at a few of the articles I realized it’s new to me. Regardless, I’m certainly happy I found it and I’ll be book-marking it and checking back frequently!

  5. I absolutely love your blog.. Great colors & theme. Did you create this website yourself? Please reply back as I’m wanting to create my very own website and would like to learn where you got this from or what the theme is named. Many thanks!

  6. I absolutely love your blog.. Very nice colors & theme. Did you create this amazing site yourself? Please reply back as I’m wanting to create my very own blog and would love to find out where you got this from or exactly what the theme is called. Appreciate it!

  7. hi!,I love your writing so so much! proportion we
    keep up a correspondence more approximately your post on AOL?
    I require an expert on this space to solve my problem.
    Maybe that is you! Having a look forward to look you.

  8. At this time it sounds like BlogEngine is the preferred blogging platform available right now.
    (from what I’ve read) Is that what you’re using on your blog?

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Articles
You May Like
Subscribe to our Newsletter
Scroll to Top