Select Page

What is Abstraction in Java?


Abstraction lets you focus on what the object does instead of how it does it. It focuses on just what is important, not any boring details.


nanadwumor
May 25, 2023

Abstraction in Java


  • Abstraction is a process of hiding the implementation details of something
  • Abstraction is a process of exposing all the necessary details and hiding the rest
  • Java achieves abstraction using abstract classes and interfaces

RECOMMENDED ARTICLES


Variables and Literals in Java
Variables and Literals in Java

Variables are used in many situations. For Instance, game applications use variables to collect your name when you sign in; to store and keep track of your number of trials, successes and failures. In Java, a variable is a container used to store...

double data type in Java
double data type in Java

The double data type is also a floating point number. It is called double because it has a double-precision decimal number.   The double data type is a floating point number. It is double precision data type The range is 4.9406564584124654 x...

The print and println methods in Java
The print and println methods in Java

The print() and println() methods are Java's predefined methods used to display text output on the console. These methods are part of the Java API. The print and println methods are used to print string of characters  onto the console The print...


Abstraction is a process of hiding the implementation details of something (a class or method) and showing only functionality to the user.

The word “Abstraction” in general is the process of working with ideas rather than working with their implementation.


Join Other Subscribers on Our YouTube Channel and Don’t Miss a thing!


Abstraction is an essential part of all object-oriented programming languages like Java, C#, C++, etc

Abstraction lets you focus on what the object does instead of how it does it. It focuses on just what is important, not any boring details.

The Driver and Car metaphor

Let’s consider this real life scenario. Consider a driver driving a car. The driver inserts the car keys, brings the engine to live and starts applying clutches, brakes, accelerators etc without necessarily thinking about the mechanics actually taking place within the car’s engine and body to cause the motion.

The driver only reacts with the car’s dashboard which is the car’s interface. What happens under the hood is abstracted away from the driver. He needs not to worry himself with it.

The Dating metaphor

To let us understand abstraction the more, let’s consider another interesting metaphor. Today,  everyone is on social media. Isn’t it?

Let’s assume you get into a blind date with a beautiful girl online, you decide to meet with her at a nearby restaurant.

What are the likely stuffs you will tell the girl on the day of meeting, just before the two of you meet? You’ll definitely tell her the name of the restaurant, the type and color of the attire you are wearing and which corner or table you are waiting for her.

At that point, you won’t bother telling her the amount of money you would give her, or any gifts you have for her or even bother to describe your father to her. Why?

Because those details aren’t important at that moment for the lady to locate you at the restaurant. So, the focus is only on the most important stuffs at the moment.

In abstraction, the programmer exposes only the necessary details and hides the rest of the body code (the so-called implementation).

Abstraction is a process of exposing all the necessary details and hiding the rest. In Java, Data Abstraction is defined as the process of reducing the object to its essence so that only the necessary characteristics are exposed to the users.

How does Java achieve Abstraction?

Java achieves abstraction using abstract classes and interfaces.

An abstract class cannot be instantiated. It serves as a Superclass to another class. Abstract classes offer partial to complete abstraction. That’s, from 0 to 100% abstraction as abstract classes can contain concrete methods.

An interface can’t be instantiated either. It is intended to be implemented by other classes.

Interfaces offer total or 100% abstraction.

Abstract classes in Java

An abstract class is a class we cannot create objects from. An abstract class is meant to serve as a Superclass to a concrete class or another abstract class. We use the keyword, abstract, to define an abstract class.

Types of Abstraction in Java

By abstracting, it helps not to break code in the future if changes or improvement becomes inevitable as it’s likely in today’s fast changing world.

The benefit of (data) abstraction is that it helps solve performance issues and improves the implementation over time.

If you make any changes in the future, say you want to improve performance, it doesn’t reflect on the code present on the client-side as the fundamental interface is not changed but only how it’s implemented is changed.

Data abstraction

Data abstraction is the process of creating a data type, usually a class, which hides the details of the data representation in order to make the data type not only easier to work with but possible to implement it in any required way.

In data abstraction, you create a representation for data that separates the interface from the  implementation (body code or working) so that the programmer or user only has to deal with the interface – that’s the commands to use, but not the internal workings of the data.

Data abstraction means reducing a particular body of data to a simplified representation of the whole.

For instance, let’s assume that you create a data type called FindValue. This data type uses keys to fetch their corresponding values. So you type a key and its value surfaces.

Example of a FindValue data type could be (Student_Name, Program). That’s, we have data in pairs where you type the student’s name and his/her position in class surfaces.

The most important idea about our data type FindValue is using keys to fetch values. That’s the interface any class that wishes to implement or use it must note.

How it’s implemented is not the problem. Whether you employ Binary Search, Linear Search, etc, just make sure you use a key to fetch a value. So we have abstracted (hidden) the implementation (how it’s done) away.

This our FindValue data type will have fields and methods that act on it, all put together as if they were in a capsule.

 In brief, at the heart of abstraction is a very important factor called encapsulation

Encapsulation in Java

By doing this, we have much leeway to change our code or add to it on how keys are used to find values.

Control abstraction

Control abstraction is the process of determining all such statements which are similar and repeat over many times and expose them as a single unit of work. 

When you write a simple if-else statement, do you care to know how flow in the program is controlled? On the human level, you explain that if (true), do this else, do that.

In reality, many complex things go on under the hood which are all abstracted away from the Java programmer.

How this if-else statement is converted to binary digits of 1’s and 0’s repeatedly anytime you invoke an if-else statement is abstracted away from the programmer. This is control abstraction.

When you type a simple mathematical statement like int x=(3 +2)×4, you are saved from getting down to the hardware and worrying yourself about how (3+2)×4 is first evaluated, converted to binary digits, stored in allocated memory and decompiled for you to see a=20 on the screen. 

The whole execution or control is abstracted away from you.

A PIE

Have A PIE. Won’t you? Object-oriented programming languages are about eating A PIE. A PIE simply means :

A=Abstraction

P=Polymorphism

I=Inheritance

E=Encapsulation

A PIE - Abstraction, Polymorphism, Inheritance, Encapsulation

You May Also Like…


double data type in Java
double data type in Java

The double data type is also a floating point number. It is called double because it has a...



0 Comments
Submit a Comment

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