Image Credit: Google/scientech easy

Hey there, until now, we have covered Inheritance, Polymorphism, and Encapsulation concepts in Java. Today, let us discuss “Abstraction” in Java which is one more important feature in OOP (Object Oriented Programming).

What do you mean by Abstraction?

When implementation details are hidden and only functionality is visible to the user, the process is termed as “Abstraction”.

Example:

As the diagram shows, ATM where a person is withdrawing cash. He inserts his card to withdraw cash or to check the amount in his account. In this scenario, implementation details are hidden from the user. He receives the cash through the machine ( which is only visible to the user). This is one of the easy examples to understand Abstraction.

How do we achieve Abstraction in Java?

There are 2 ways by which we can achieve abstraction in Java.

By Abstract class:

Before understanding abstract class, let's see what is Abstract method and some important pointers.
1. A method without a body ( no implementation) is called as Abstract method.

2. We use the “abstract” keyword to define classes and interfaces.

3. In abstract classes, we can have abstract methods as well as concrete methods.

4. We can not instantiate an abstract class.

5. If any class extends an abstract class, then the class has to implement all the abstract methods of the abstract parent class or it has to be declared abstract as well.

Example:

public class CoffeeMachine
{
no.of door = 2;
start()
{
System. out.println(“Starts with pressing C”);
}

public class WaterMachine
{
no of door = 1;
start()
{
System.out.println(“starts with finger touch”);
}

Here, let’s create an Abstract class
abstract Class VendorMachine
{
int no_of_door;
abstract void start();
}

Here, we created an abstract class name VendorMachine where we have defined an abstract method start() whose implementation details are hidden.

We should be defining an abstract method in a class that inherits the class. Meaning, we will have to provide implementation details in this abstract method like below.

public class CoffeeMachine extends VendorMachine
{
void start()
{
System. out.println(“Starts with pressing C”);

Here, start() is an abstract method which is defined in a class that is inherited from the main class.

By using Interfaces:

2. It contains only abstract methods. It specifies what a class should do.

3. It is used to achieve abstraction.

4. It supports multiple inheritances.

5. It can be used to achieve loose coupling.

Syntax:

interface <InterfaceName>

{

methods where only public abstract methods are allowed. But, incase if you do not explicitly define public abstract in a method, then complier will automatically consider it as an abstract method with access modifier as public.

fields where by default public static final is allowed.

In Java 8 version and proceeding versions, we can create default concrete methods.

example:

default void display();

We can create static methods in Interfaces as well.

example:

static void show();

In Java version 9, we can create private methods in Interfaces as well. That's all about

I hope, this will help you in understanding Abstraction and the ways to achieve abstraction in Java. You can start writing with some basic code to implement abstraction for practice purposes.

Please feel free to share your inputs. Let's learn, grow and improve together as software testing professionals. If you like it, hit a clap or a comment will inspire me more. Thank you for stopping by!

QA professional with nearly 10+yrs of testing experience.Expertise in functional, web automation,Mobile API testing,Leader,Learner,Mentor & Inspiring QA.