Abstraction in Java

Abstraction in Java with Real-time Examples

In this article, I am going to discuss Abstraction in Java with Examples. Please read our previous where we discussed Inheritance in Java with Examples. At the end of this article, you will understand what is Abstraction in Java and when and how to implement Abstraction in Java with examples.

Note: What we will do here is, first we will create a real-time example without using Abstraction, then we will identify the problem, and finally we will see how to overcome the problem using abstraction in java.

What is Abstraction?

Data Abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details. Abstraction is nothing but the quality of dealing with ideas rather than events. Abstraction is selecting data from a larger pool to show only the relevant details of the object to the user.

In more simple words we can say that Abstraction in Java is a process of defining a class by exposing the necessary and essential (compulsory) details to the outside world by hiding the unnecessary things (or complexity or implementation details). That means you only need to expose what is necessary and compulsory and need to hide the unnecessary things to the outside world. In Java, by using the private access modifiers, you can hide the member of a class.

Types of Abstractions:

Typically abstraction can be seen in two ways:

  1. Data Abstraction
  2. Control Abstraction
Data abstraction:

Data abstraction is the way to create complex data types and exposing only meaningful operations to interact with the data type, whereas hiding all the implementation details from outside works. The benefit of this approach involves the capability of improving the implementation over time e.g. solving performance issues is any. The idea is that such changes are not supposed to have any impact on client code since they involve no difference in abstract behavior.

Control abstraction:

Software is essentially a collection of numerous statements written in any programming language. Most of the time, statements are similar and repeated over places multiple times. Control abstraction is the process of identifying all such statements and expose them as a unit of work. We normally use this feature when we create a function to perform any work.

Real-Time Example of an Abstraction:

Suppose you want to create an ATM Machine and you are asked to collect all the information about the operations on the ATM Machine. There are chances that you will come up with the following information.

  • Cash Withdrawal
  • Money transfer
  • Mini statement

You might have used ATM machines many times for cash withdrawal and bank statements. The ATM Machine has given a card swipe slot, a touch screen, and a keyboard, you can use these features to withdraw the money but you never know what exact actions or operations takes place internally in the machine-like after a card swipe the machine checks whether the card is valid or not, then the PIN will be verified if it is correct then only we can withdraw the money but the withdraw money should not be greater than the available balance.

Example: Implementing ATM Machine

Let us implement the above-discussed example using java. The following is our ATMMachine class and if you further notice, here we declared each and every method as public.

class ATMMachine
{
  public void Enter_Card()
  {
    System.out.println ("Card Verification");
  }
  
  public void Enter_Pin()
  {
    System.out.println ("Pin Verification");
  }
  
  public void Cash_Withdrawal ()
  {
    System.out.println ("To withdraw cash from ATM");
  }
  
  public void Validate_Withdraw_Amount()
  {
      System.out.println ("Validate the Amount to be withdraw");
  }
  
  public void Update_Amount()
  {
      System.out.println ("Update the Amount after withdraw");
  }
  
  public void Cash_Dispose()
  {
    System.out.println ("Disponse the cash from ATM");
  }

  public void Mini_Statement ()
  {
    System.out.println ("Get the mini statement");
  }
}

As you can see, in the above code, now the user of our ATMMachine class can call the methods and perform the necessary actions as shown in the below code.

public class Main
{
  public static void main (String[]args)
  {
      ATMMachine am = new ATMMachine ();
      //Accessing the Public Properties and methods
      am.Enter_Card();	
      am.Enter_Pin();
      am.Cash_Withdrawal();
      am.Validate_Withdraw_Amount();
      am.Update_Amount();
      am.Cash_Dispose();
  }
}

Output:

Abstraction in Java with Real-time Examples

As you can see in the above image, you get the output as expected. Now, let us move and analyze the code. Do, we really need to call the following three methods manually.

Validate_Withdraw_Amount();
Update_Amount();
Cash_Dispose();

The way we have implemented the code in our ATMMachine class if the withdrawal amount is less than the available balance, then also it is going to perform the withdrawal operation. At the same time, the user of the ATMMachine class may forget to call some of the methods as shown in the below code.

public class Main
{
  public static void main (String[]args)
  {
      ATMMachine am = new ATMMachine ();
      //Accessing the Public Properties and methods
      am.Enter_Card();	
      am.Enter_Pin();
      am.Cash_Withdrawal();
      am.Cash_Dispose();
  }
}

Output:

Abstraction in Java

Why this is happening?

Because we are not following the Abstraction Principle. As we made all the methods are public, so the user of the ATMMachine class can access all these methods.

How to solve the above problem?

By using the abstraction principle. The methods which you want to expose, make them as public and the methods which you don’t want to expose, make them as private. So, let us first modify the ATMMachine class as shown below.

class ATMMachine
{
  public void Enter_Card ()
  {
    System.out.println ("Card Verification");
  }

  public void Enter_Pin ()
  {
    System.out.println ("Pin Verification");
  }

  public void Cash_Withdrawal ()
  {
    System.out.println ("To withdraw cash from ATM");
    Validate_Withdraw_Amount ();
    Update_Amount ();
    Cash_Dispose ();
  }

  private void Validate_Withdraw_Amount ()
  {
    System.out.println ("Validate the Amount to be withdraw");
  }

  private void Update_Amount ()
  {
    System.out.println ("Update the Amount after withdraw");
  }

  private void Cash_Dispose ()
  {
    System.out.println ("Disponse the cash from ATM");
  }

  public void Mini_Statement ()
  {
    System.out.println ("Get the mini statement");
  }
}

As you can see in the above code, the necessary methods and properties are exposed by using the “public” access modifier whereas the unnecessary methods and properties hidden from outside the world by using the “private” access modifier. We made the following methods as private, so they are not exposed to the user of the ATMMachine class. As part of the Cash_Withdrawal, we need to put the required logic to call methods.

Validate_Withdraw_Amount();
Update_Amount();
Cash_Dispose();

Now, modify the Main class as shown below.

public class Main
{
  public static void main (String[]args)
  {
      ATMMachine am = new ATMMachine ();
      //Accessing the Public Properties and methods
      am.Enter_Card ();
      am.Enter_Pin ();
      am.Cash_Withdrawal ();
  }
}

Output:

Java Abstraction

Now, from the Main class (or the user of the ATMMachine class), if you try to call the following methods then you will get a compile-time error.

Validate_Withdraw_Amount();
Update_Amount();
Cash_Dispose();

Now, we are exposing the necessary methods (Enter_Card, Enter_Pin, and Cash_Withdrawal) to the outside world or to the user of the ATMMachine class by hiding its implementation details or the Complexity (Validate_Withdraw_Amount, Update_Amount, and Cash_Dispose). This is called abstraction in Java.

Advantages of Java Abstraction:
  • It reduces the complexity of viewing things and increases the readability of the code.
  • Helps to increase the security of an application or program as only important details are provided to the user.
Difference Between Abstraction and Encapsulation in Java:

Abstraction

Encapsulation

Refers to showing only the necessary details to the intended users. Means to hide (data hiding). Wrapping and just hiding properties and methods.
Used in programming languages to make abstract classes. Used to hide the code and data in a single unit to protect the data from the outside world.
It is implemented using abstract classes and interfaces. It is implemented using a private and protected access modifier.

In the next article, I am going to discuss Abstract Classes and Abstract Methods in Java in detail. Here, in this article, I try to explain Abstraction in Java with Examples. I hope you enjoy this Abstraction in Java with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

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