OOP Concept for Beginners: What is Abstraction?

A key concept in Object-Oriented Programming (OOP) is Abstraction. And it is also one of the most confusing ones.

Abstraction in Object-Oriented Programming refers to “showing” only the essential attributes of something and “hiding” any implementation information that is unnecessary to the user.

It allows users not to get overwhelmed by the hidden logic that makes the users complex logic. Thus, OOPs abstracts hidden logic so that the user is not required to understand or even think about it.

But first, let us give you an overview of OOPs.

What is OOP?

OOP or Object-Oriented Programming is a type of programming that is fundamental to the programming paradigm.

It is the most popular programming methodology which is adapted by many programmers during their educational career.

Unlike procedural programming, object-oriented programming has various advantages:

  • OOPs keep the Java code DRY, i.e., “Don’t Repeat Yourself,” by making the code easier for maintenance, modification, and debug. This means a programmer can reduce code repetition by extracting out common code and placing them in a single place.
  • It is faster and has easier execution than procedural programming.
  • You can develop reusable applications in a shorter development time containing less code with the help of OOPs.
  • It gives a clear program structure.

There are two basics to Object-Oriented Programming: Class and Object.

  1. Class – It is a template for objects or a blueprint to create specific objects. For instance, a broad category like a dog is a class that defines the attribute like color, breed, name, etc., that the objects or instances of the class.
  2. Object – Objects are class instances that inherit all the variables and methods from a class. For example, a class – Dog has objects like Dalmatian, German Shepherd, St. Bernard, Golden Retriever, etc.

OOPs have four pillars – Inheritance, Abstraction, Encapsulation, and Polymorphism.

  1. Inheritance – When a child object inherits all the properties and behaviors of a parent object, it is called inheritance. For example, a dalmatian puppy will also have the same features as its parents.
  2. Abstraction – Hiding unnecessary details and functionalities and only showing what is important to work is an abstraction. For example, the bones, muscles, and other stuff are hidden inside our skin, which provides an abstraction to dogs and allows them to do other actions and activities.
  3. Encapsulation – Encapsulation means Tying the code with data in a single unit or binding them together. Classes and objects are encapsulated in Java and other OOPs languages. For example, the dog is a class that encapsulates all types and breeds of dogs.
  4. Polymorphism – To execute a single task in various ways is called polymorphism. For example, “to speak” is a method but is performed differently: a dog barks, a human speaks, a cat meows, a bird chirp, etc.

This article focuses on only one OOPs concept – Abstraction.

What is an Abstraction in OOP?

In OOPs, abstraction is one of the most important concepts that allows programmers to focus on only the aspects important to the system under consideration.

READ  10 Best Free and Open Source Call Center Software in 2021

Abstraction allows the programmer to shift their focus from implementation details of actions towards the classes, available methods, etc., to make programming simpler.

We can understand abstraction using examples of inheritance and encapsulation:

In an inheritance hierarchy, the parent classes or higher classes contain simple and general implementation, but the child class or lower class contain more detailed implementation of the higher classes. This is an abstraction.

When a programmer hides the implementation details of one class from other classes in encapsulation, the class is simpler.

Example

Nothing can be better understood without examples. Examples provide a practical approach that allows anyone to understand better what someone is talking about.

Let us consider a few real-world examples to understand abstraction.

1. Coffee machine

More than 1.4 billion cups of coffee are poured worldwide in a day, and about 45% of it comes from the USA. So, we can say that coffee is one of the most loved beverages in this world.

The coffee houses make coffee with a coffee machine’s help by inputting water and coffee beans in it. They switch the machine on and then select the kind of coffee required, after which the machine brews the most delicious coffee for you.

But did you ever stop and found out how the machine brews the coffee. What is the ideal temperature that it uses? What is the amount of coffee that it puts in?

No. Right?

Thus, the coffee machine abstracts its internal functioning and allows you to brew your cup of coffee and enjoy it.

The same is the concept used in object-oriented programming languages like java. The internal functioning of the system is abstracted from the programmer so that they can enjoy programming.

2. Car

A car is another real-world example of abstraction.

The car is a complex system made of various items such as the engine, battery, radiator, front and rear axle, transmission system, steering system, suspension system, electrical system, and many others.

However, there is no need to know all about these parts to operate the car.

Using a car is as simple as turning the key in the ignition or pressing the start button and then driving.

There is no necessity to understand how the car engine gets started, the fuel system, electric system, etc. You can start your car and drive away.

3. Microwave

A microwave is a complex machine on the inside but has a fairly simple and easy to use interface.

The touch panel or the buttons are all that we interact with to cook, reheat, bake, warm, and grill our food.

In today’s, microwaves can be started using mobile phones and have lots of pre-cook menu features such as baked cake, warm milk, spaghetti, make pizza, etc., which have pre-set temperature and time settings. We only have to select the option and press the start button.

There is no need to understand how the food is being cooked, how the microwaves work, how the temperature settings work, etc.

We use the microwave and focus on other important aspects such as what flavor the cake would be, ingredients for tea/coffee, reheat timing, and other cooking menus.

This is another great example of abstraction, which helps us to understand object-oriented programming.

Now let us understand OOP with the help of one technical example.

4. Passport Application

While creating a passport application, you need to access data about a customer from the database, which contains various kinds of details related to various customers.

The records in the customer table contain information related to customers in the following columns:

  • First Name
  • Last Name
  • Address
  • Contact Number
  • Favorite Foods
  • Hobbies
  • Favorite Movies
  • Favorite Songs
  • Proof of U.S. citizenship
  • Government-issued photo ID
READ  Top 20 Subversion SVN Interview Questions & Answers

However, not all the information contained in these columns are relevant for getting a passport.

Records related to favorite foods, hobbies, favorite songs, and favorite movies are not required while applying for a passport.

Thus, when only the required fields are selected from the pool of data, abstraction can be seen as we fetched, selected, and/or removed customer related information.

This information can be used for various other uses such as banking applications, hospital applications, government databases, portal information, etc., without much modification.

This is an advantage of Abstraction in Object-oriented programming (OOP).

With these four examples, we hope you can understand OOPs a little better. Let us now understand how abstraction is done in java.

Abstraction in Java

In java, abstraction can be achieved with the help of interfaces and abstract classes.

1. Interface

The interface is like a class blueprint that has methods and variables. However, the methods declared in an interface contain nobody. The methods and variables declared in the interface must be implemented by the class that implements the interface.

For instance, an interface “Fun” contains the following:

Interface Fun {
play()
}

The class Fun implementing interface Fun must implement play().

In java, the interface is declared with the “Interface” keyword’s help and is used to provide total abstraction.

Thus, all interface methods are public and have an empty body with all fields being public, static, and final by default.

Syntax of the interface:

Interface <name_of_interface>
{
          //declare variables
          //declare abstract methods
}

A class can implement an interface and can be implemented using the “implements” keyword. This class must implement all the methods and contain all the variables declared in the interfaces which it is implementing.

Importance

  • Loose coupling and total abstraction can be achieved using interfaces.
  • Interfaces contain final, static, and public variables, giving total abstraction, whereas abstract classes give partial abstraction.
  • In java, it is impossible to perform multiple inheritances, but multiple inheritances can be achieved with the help of interfaces.

As an update in Java 8, default and static methods were added to the interfaces, and as part of Java 9, private methods were added to the interfaces.

2. Abstract Class

“Abstract” is a keyword in java used to provide abstraction as part of object-oriented programming. It is a non-access modifier.

Abstract Class – is a class that partially implements abstraction in Java. Here, partial abstraction means that unlike interfaces, not all methods have a method definition in an abstract class.

Abstract class objects cannot be instantiated, and thus any subclass that implements an abstract class must implement all of its methods, or the subclass must be declared an abstract class itself.

The syntax for abstract class is:

abstract class <class_name>
{
          //class body
}

Abstract Methods – are methods that have only definition but no implementation. They are declared in an abstract class using an “abstract” type modifier.

Abstract methods are also called subclasses responsibility because they are also dependent on the class implementing them as they are part of an abstract class.

The syntax for an abstract method is:

Abstract return_type <name-of-method>(parameter_list);

There are a few rules concerning abstract methods:

An abstract method can only be declared within an abstract class. Thus, any class containing either one or more abstract methods must be declared abstract.

There are a few illegal combinations between other modifiers and abstract modifier that should be acknowledged –

  • Abstract native
  • Abstract strictfp
  • Abstract static
  • Abstract synchronized
  • Final
  • Abstract private

Now, the question arises, if we have interfaces, when do we use abstract methods and class?

READ  Top 10 Best Mobile App Security Testing Tools In 2021

When two or more subclasses have a similar piece of code doing the same thing but are implementing it differently, abstract methods are declared.

With interfaces, you can only define the functionality but cannot implement it, whereas, with an abstract class, subclasses can implement or override the methods.

Difference

We are now familiar with abstraction in java and how we can achieve it. Let us now consider a few differences to clarify our confusion about how abstraction is different from encapsulation and why we need abstract class and interfaces.

1. Abstraction and Encapsulation

Abstraction and Encapsulation are part of the four pillars of OOPs – inheritance, encapsulation, polymorphism, and abstraction.

However, unlike the other two, most people always get confused between encapsulation and abstraction because they have a similar meaning.

Let us check out the difference to understand better.

AbstractionEncapsulation
Hiding unwanted details while showing essential details is called abstraction in programming.In programming, encapsulation means binding the code and data together into a single unit.
With abstraction, a programmer can focus on what information an object must contain.Encapsulation allows hiding of the internal details and mechanics behind the working of an object to ensure security.
We can solve design level problems with the help of abstraction in object-oriented programming.We can solve implementation problems with the help of encapsulation in object-oriented programming.

2. Abstract Class and Interfaces

In Java, abstraction can be achieved by both abstract class and interface.

Interfaces allow for a 100% abstraction, and abstract class only allows partial abstraction, but these types have their pros and cons.

We require them both in java to perform abstraction. Let us understand how they are both different from each other.

Abstract ClassInterface
An abstract class is a java class with an abstract keyword containing final, static, and static final variables with any of the access specifiers.The only public static final variable is allowed for interfaces.
Multiple inheritances are not supported in the abstract class.The interface supports multiple inheritances.
There can be both abstract and non-abstract methods inside an abstract class.In an interface, only abstract methods are allowed.
Protected and abstract public methods are possible in an abstract class.Only abstract public methods are allowed in an interface.
An abstract class can implement an interface.An interface cannot implement an abstract class.

Advantages of Abstraction

In object-oriented programming, abstraction allows a programmer to reduce the design and software process implementation complexity.

Several related classes can be grouped as siblings using abstraction in java programming.

Abstraction allows a programmer to avoid duplication of code in a program and increase reusability.

With abstraction, an application or program’s security is increased as programmers are only allowed access to details important to them.

Summary

Abstraction is one of the most helpful concepts present in all object-oriented programming languages such as C++, Java, C#, etc.

It is critical that as a programmer, you understand abstraction and how to achieve it, and we hope this article has been helpful to you.

Frequently Asked Questions (FAQs)

What are abstraction and encapsulation?

Abstraction – It is a basic OOPs concept that implies that the user is shown only the details which are important to them, and all other unnecessary details are securely hidden.
Encapsulation – It is also a basic OOPs concept. Encapsulation means binding together data and code into a single entity so that the details can be protected from the outside world.

What is abstraction?

Based on the Cambridge dictionary, abstraction means a vague situation that is not based on real events.
However, in programming, abstraction means hiding unnecessary details from the user and showing them enough to facilitate their focus on other complex problems.

Give some real-life examples of abstraction?

Some real-life abstraction examples are ATM (all we do is click some buttons and cash flows), microwave, car, etc.

What is an example of abstraction in programming?

In the programming world, we can see abstraction if we observe the programming languages. We went from machine languages and slowly added layers of abstraction to hide the machine’s internal functions as we reached high-level languages. This allowed us to move from simple mathematics calculation towards more complex problems.

What are the 4 basics of OOP?

The four basics of OOPs are Inheritance, Encapsulation, Polymorphism, and Abstraction.

Recommended Articles