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.
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!
Thank you for sharing us… If you don’t mind share us with your friends’ Thanks
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.
Thank you for Reaching us https://ennicode.com/ , Sorry for the late reply, and anyway please contact us anytime
Excellent article. I will be going through some of these issues as well..
Thank you for Reaching us https://ennicode.com/
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!
Thank you for Reaching us https://ennicode.com/ , If you don’t mind share us with your friends’ Thanks
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!
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!
Thank you for Reaching us https://ennicode.com/ , Yes this is done myself, and used Astra themes (https://wpastra.com/?bsf=10048) also used Elementer page builder (https://trk.elementor.com/liyl0yq1oh7n) for built website .Also if you like we can develop a WordPress site for you (check our services: https://ennicode.com/services/). Thank you…
Thank you for Reaching us https://ennicode.com/ , If you have time, share us with your friends’ Thanks
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!
Thank you for Reaching us https://ennicode.com/ , Yes this is done by me, and used Astra themes (https://wpastra.com/?bsf=10048) also used Elementor page builder (https://trk.elementor.com/liyl0yq1oh7n) for the built website. Also if you like we can develop a WordPress site for you (check our services: https://ennicode.com/services/). Thank you…
Really appreciate you sharing this blog post.Much thanks again. save writing.
Thank you for Reaching us https://ennicode.com/, If you have time, share us with your friends if you wish to receive new posts please subscribe Thanks
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.
Thank you for Reaching us https://ennicode.com/, If you have time, share us with your friends if you wish to receive new posts please subscribe Thanks
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?
Thank you for Reaching us https://ennicode.com/, If you have time, share us with your friends if you wish to receive new posts please subscribe Thanks
In it something is. I will know, many thanks for the information.
Thank you for Reaching us https://ennicode.com/, If you have time, share us with your friends if you wish to receive new posts please subscribe Thanks
This is a great tip especially to those fresh to the blogosphere. Short but very accurate information… Many thanks for sharing this one. A must read post!
Thank you for Reaching us https://ennicode.com/, If you have time, share us with your friends if you wish to receive new posts please subscribe. use the contact us page for more info Thanks