Abstraction is one of four important pillars of OOP. But that’s not how we will explain abstraction. In this post, we will see meaning of abstraction with reference to the real world. We have tried to give most relevant, real world examples.
Oh, and if you are interested in learning Java, then you should also read the last few paragraphs which talk about how to implement Abstraction in Java.
And if you are not interested, you can just skip the last part of this article and follow us on Facebook 🙂
A lot new Java learners are often confused because when they learn Java from different sources, they all provide abstraction explanation but none of them makes it easy.
From a engineering student’s perspective, solid understanding of Abstraction goes a long way in not just interviews but also programming in general. Enterprise applications and products use abstraction to simplify a lot of functionality. Even in any basic core or advanced java syllabus, you have already used Abstraction without noticing.
Abstraction is explained as ‘information hiding’. The questions that arise here are:
- What are we hiding?
- From whom are we hiding?
- What is the need of hiding?
We hide unnecessary functionality from users. This unnecessary functionality is such that it is required for the successful working of the program but is completely unnecessary for the user to know the complete details about it. In this way, the user can focus on how to make the best use of the application without worrying about details of how the program is working.
In terms of OOP (Object Oriented Programming), Abstraction provides a lot of simplicity to the applications as modules stay independent of each other. Systems can be designed in such a way that large scale changes in one module will cause minimal or zero ripple effect changes in the linked modules.
If, by now, you have some idea about meaning of abstraction, then you’re on the right track. Let us take a real world example of a car to better understand Abstraction.
Abstraction in a Car
A car is more than just a steering wheel, acceleration, clutch and brake pedals, gearbox, indicators etc. that are visible to the driver. These are the components that are required to run the car and the driver is expected to fully know how they all work. However, the driver is not expected to know how the fuel enters the carburetor system or how the force is transmitted to the wheels etc.
The car has hidden the actual implementation of the system and only focusing the user to make use of what has been provided to him (i.e. steering wheel, acceleration, brake pedals etc.). The car has created a layer of abstraction.
This has been done in such a way that even if a user drives multiple cars, he will feel minimum or no difference between the working of other cars.
Let us take another example which implements a similar technique of hiding.
Abstraction in the Internet
When we want to visit a web page, we type the name of the website in the URL address bar. On the internet though, the servers are given unique numerical identifiers called IP address. So, in the background, the client browser sends the domain name part of your URL to a DNS (Domain Name System)Server.
The DNS server translates the name of the website to its numerical IP Address. This IP Address is sent back to the client browser (i.e. your browser). The browser then simply forwards your request to the server whose IP address was just received.
The actual content server then send back the response in HTML. The web browser interprets the HTML tags and displays the page on your screen. This is just a brief idea of what is going on in the background, which is invisible to the naked human eye.
Normally, we all see this as a one-loop operation, but it is actually a two-loop operation. Even though it is an important process, we are only concerned with the web page we have requested. An average user does not care about these details; he just wants to browse the internet, and download stuff.
So the web browser is successful in hiding these details from the user and making them focus only on using the internet. Here, the browser is abstracting the DNS resolution details from user.
Relation between the two examples of Abstraction
- In both the examples, we see that, even though the processes that take place in the background are necessary, they are kept hidden from us.
- The main objective in both the cases is fulfilled.
- There is minimum or no interaction between the user and the background process.
- The user’s focus is solely on making the most of the application. This is only achieved when we keep the user’s focus away from the actual process, and more towards the functions of the application.
How does Abstraction apply in Software?
In software, abstraction reduces dependency among your modules. A specific module will see only the required functionality of the other modules it is dependent on. Hence, implementation changes in the dependent modules will have minimal effect on the other. Less changes because of this “ripple effect” will result in less effort during any modification. This will make the maintenance easier.
Almost always, there will be multiple modules in an application. If changes done in one module does not require any change in its dependent module, we say that the coherence in your application is high. Abstraction helps us achieve this coherence.
Why is Abstraction in software important?
If you have every worked in a team, you will understand this point easily. Every team member has a defined role, some important roles require multiple team members.
Consider this situation: Sara, Peter, and Jack are a part of Application Processing Team at a large conference. Let’s call them Person 1 (Sara), Person 2 (Peter), and Person 3 for the sake of understanding this.
Since they are all working toward a common goal, they will be sharing some information, which will be the application form. They can pass this form in stages from one member to another. For example, Sara (Person 1) enters the form data into the software, Peter (Person 2) approves/rejects the application and Jack (Person 3) processes the accepted entries. This is the normal scenario and has been working well.
But if Peter also performs the role of an application processor, both Peter and Jack (Person 2 & 3) will be working on processing the approvals. All this while Peter (Person 2) is simultaneously allocated on approving/rejecting the entries. This slows down the entire application process.
All was working fine until we added some extra responsibilities for Person 2 (Peter) and caused the extra burden on one of people involved in application processing.
Imagine these people involved in application processing are like the modules in your application. If a software module shares its functions and processing with other modules, it will result in one royal mess of a code. And by this I mean, when you will try to update one module, the complex dependency links among the code will cause those changes to be reflected in other modules also. This is caused as “ripple effect”.
In software, applications do not die. They are upgraded every few years. Some technical architects like to keep them updated every quarter. Often business owners will request for a change or addition of a new feature. Whatever may be the updating frequency, making changes in multiple modules all the time will only slow it down. And we do not want that!
The dependence among application modules cannot be more than what is absolutely essential. One module should do ONLY one task and do it with as much independent from others as possible.
Hope this explanation about meaning of abstraction was easy for you. We will now see how to implement abstraction in Java.
Implementing Abstraction in Java: Abstract Classes & Interfaces
Java Developers use the concept of abstraction so that they can keep the user’s focus on making the most of the application provided to them. There are two ways that developers can implement abstraction.
We will explain about Abstract Classes & Interfaces in different articles. For now, it is important to understand the concept of abstraction.
In Java, Abstract Classes & Interfaces both provide us abstraction. An Abstract class can contain abstract as well as concrete methods, whereas all methods in an interface are by default public and abstract. So, at a design level, we can say Interfaces provide 100% abstraction and Abstract classes provide less than 100% abstraction.
Also, in Java we cannot create a class with two parent classes since Multiple Inheritance is prohibited. But a class can implement multiple interfaces at a time if it has to participate in multiple hierarchies. Thus, Interfaces are also a way to implement abstracted multiple inheritance in Java.
Hope this post about meaning of abstraction was easy for you. We aim to spread knowledge so that you don’t have to read it twice to understand it.