Updated April 5, 2023
Introduction to Abstraction in OOPs
Abstraction is one of the most important concepts of OOPs; it helps to hide the complexity from the user and helps to show only the important service to the user. WE have Java programming language, which makes use of Abstraction and is able to hide the complicated and complex code from the user; we are only supposed to provide the services which are useful to the user; in the real world, we have many explanations which can define the usage of Abstraction in an object-oriented programming language. Let’s taken a list of examples, ATM functionality, Phone calls, Banking functionality provided to users, and many more. We do not know the internal implementation of how they are working, but we are only supposed to use those services provided by them to avoid all the complexity. In the coming section, we will continue its internal working and how we can implement this to make the application more efficient.
Syntax
As we know that it is a concept of object-oriented programming language, let’s take a look at the syntax which we can use to achieve abstraction in the program, see below;
1) We can achieve Abstraction through Abstract class: syntax for this as follows:
abstract class Your_class_name{
// your logic goes here
}
2) We can use interface to achieve abstartcion: syntax for this as follows:
interface name_of_interfcae{
// your logic goes here
}
As you can see, we can achieve abstraction in Java by using abstract classes and interface; in the coming section, we will see a more detailed explanation of these both for beginners to understand the concept in more detail.
How does Abstraction work in OOPs?
As we already know that from the previous section why we use abstraction, let’s look at how it works internally to make this happen. In a real-world scenario, we have so many examples which make use of abstraction internally. It helps us hide the complex detail from the user, and it helps us show only the important part to the user. Because if we show them the implementation, then they will do not understand anything. It basically provides the user only the important part of the implementation of that meat for the user. In this section, we will take an example to understand its internal implementation; let’s get started;
1) Suppose we go to ATM to withdraw money from it; at the start, we only know how it works. Nut internally, it processes many things like it uses servers, databases, and many more things to process your data. It provides us with so many functionalities which helps us to perform the desired operation in less time. I want to withdraw some money, so I will, as a user will select the withdraw option and withdraw the money I want. But internally, we do not know how it is working, so it is hiding from the user to keep it simple and provide them with the necessary service they want.
2) Also, we have one more exhale of mobile how it works, at the time of receiving the call, we just receive it and answer the call. But internally, we do not know how it is working; they are also hiding the user’s internal complexity and only providing the phone pickup service to the user. We have so many more options where we can see the complexity of code, and their work is hidden from the user.
We can achieve abstraction by the use of abstract class and interface; let’s take a closer look at both of them to see below.’
1) Abstract class: If we talk about the abstract class, it is used to provide the Abstraction but partially because it contains the abstract method and non-abstract method as well. That means all the things are not hidden from the user. To provide the implementation to the Abstract class, we have to make one normal class where we can provide the implementation to the abstract class and provide our own implementation for the abstract method. Below see the reference code for better understanding see below;
e.g. :
abstract class DemoAbs{
abstract void show(); // abstract method
public void getm(){
// normal method ..//
}
}
2) Interface: We can also use an interface that helps us provide the Abstraction in OOPs 100%; because the method defines inside the interface is always abstract, we use the ‘implements’ keyword to implement the interface in java. Let’s take, and a closer look at the code reference for interface see below;
e.g. :
Interface DemoInt{
void getm();
void getb(); // by default abstract
}
In this way, we can achieve Abstraction in OOPs; they have their own benefit over one another depending on our requirement.
Examples of Abstraction in OOPs
A simple example to understand Abstraction in OOPs is mentioned below:
Example:
a) abstarct classs code ;
e.g.:
package com.practise.tets;
public abstract class AbstractClassexmaple {
public static final String strTest = "Normal final string to check";
String msg = "";
AbstractClassexmaple(String msg){
this.msg = msg;
}
abstract void m1();
abstract void m3();
public static void m2(){
System.out.println("I am static method with body");
}
private void privateMethodCheck() {
System.out.println("i am priate method from abstract class !!");
}
}
b) class providing the implementation to the methods :
e.g.:
public class AbstarctImpl extends AbstractClassexmaple{
AbstarctImpl(String msg) {
super(msg);
}
@Override
void m1() {
System.out.println("First method implementation to the class");
}
@Override
void m3() {
System.out.println("Second method implementation to the class");
}
}
c) Abstartct class Test class to check it is working;
e.g. :
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class AbstractClassTest {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
AbstarctImpl abstarct = new AbstarctImpl("testing constructr");
abstarct.m1();
abstarct.m3();
AbstarctImpl.m2();
System.out.println(AbstractClassexmaple.strTest);
}
}
d) Run the main class to see the output of the above code see below;
Output:
Conclusion
As we saw that Abstraction is used to hide the internal complexity from the user and show only the useful part to the user. Also, we can provide our own implementation to the methods we have, which means specific implementation that makes the code readable and stable.
Recommended Articles
We hope that this EDUCBA information on “Abstraction in OOPs” was beneficial to you. You can view EDUCBA’s recommended articles for more information.