Tutorial

What is Abstraction in OOPS?

Published on August 3, 2022
author

Pankaj

What is Abstraction in OOPS?

Abstraction is one of the core concepts of Object-Oriented Programming. Abstraction defines a model to create an application component. The implementation of abstraction depends on the language-specific features and processes.

1. What is Abstraction?

Abstraction is the process of hiding the internal details of an application from the outer world. Abstraction is used to describe things in simple terms. It’s used to create a boundary between the application and the client programs.

2. Abstraction in Real Life

Abstraction is present in almost all the real life machines.

  • Your car is a great example of abstraction. You can start a car by turning the key or pressing the start button. You don’t need to know how the engine is getting started, what all components your car has. The car internal implementation and complex logic is completely hidden from the user.
  • We can heat our food in Microwave. We press some buttons to set the timer and type of food. Finally, we get a hot and delicious meal. The microwave internal details are hidden from us. We have been given access to the functionality in a very simple manner.

3. Abstraction in OOPS

Objects are the building blocks of Object-Oriented Programming. An object contains some properties and methods. We can hide them from the outer world through access modifiers. We can provide access only for required functions and properties to the other programs. This is the general procedure to implement abstraction in OOPS.

4. What are the different types of abstraction?

There are two types of abstraction.

  1. Data Abstraction
  2. Process Abstraction

4.1) Data Abstraction

When the object data is not visible to the outer world, it creates data abstraction. If needed, access to the Objects’ data is provided through some methods.

Data Abstraction
Data Abstraction

4.2) Process Abstraction

We don’t need to provide details about all the functions of an object. When we hide the internal implementation of the different functions involved in a user operation, it creates process abstraction.

Process Abstraction
Process Abstraction

5. Abstraction in Java

Abstraction in Java is implemented through interfaces and abstract classes. They are used to create a base implementation or contract for the actual implementation classes. Car.java: Base interface or abstract class

package com.journaldev.oops.abstraction;

public interface Car {

	void turnOnCar();

	void turnOffCar();

	String getCarType();
}

ManualCar.java, AutomaticCar.java: implementation classes of Car.

package com.journaldev.oops.abstraction;

public class ManualCar implements Car {

	private String carType = "Manual";
	
	@Override
	public void turnOnCar() {
		System.out.println("turn on the manual car");
	}

	@Override
	public void turnOffCar() {
		System.out.println("turn off the manual car");
	}

	@Override
	public String getCarType() {
		return this.carType;
	}
}
package com.journaldev.oops.abstraction;

public class AutomaticCar implements Car {

	private String carType = "Automatic";

	@Override
	public void turnOnCar() {
		System.out.println("turn on the automatic car");
	}

	@Override
	public void turnOffCar() {
		System.out.println("turn off the automatic car");
	}

	@Override
	public String getCarType() {
		return this.carType;
	}
}

User Program: Let’s look at a test program where the Car functions will be used.

package com.journaldev.oops.abstraction;

public class CarTest {

	public static void main(String[] args) {
		Car car1 = new ManualCar();
		Car car2 = new AutomaticCar();

		car1.turnOnCar();
		car1.turnOffCar();
		System.out.println(car1.getCarType());

		car2.turnOnCar();
		car2.turnOffCar();
		System.out.println(car2.getCarType());

	}

}

The client program only knows about the Car and the functions that the Car provides. The internal implementation details are hidden from the client program. References: Wikipedia, Oracle Docs

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
February 15, 2021

Nice Explanation ,but need more details

- S Sarath

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    June 4, 2020

    Difference between Abstraction and Encapsulation Abstraction solves the issues at the design level. Encapsulation solves it implementation level. Abstraction is about hiding unwanted details while showing most essential information. Encapsulation means binding the code and data into a single unit. Abstraction allows focussing on what the information object must contain Encapsulation means hiding the internal details or mechanics of how an object does something for security reasons.

    - Mukthiyar

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      June 4, 2020

      Difference between Abstract Class and Interface An abstract class can have both abstract and non-abstract methods. The interface can have only abstract methods. An abstract class does not support multiple inheritances. An interface supports multiple inheritances. An abstract class can provide the implementation of the interface. An interface can not provide the implementation of the abstract class. An abstract class can have protected and abstract public methods. An interface can have only have public abstract methods. An abstract class can have final, static, or static final variable with any access specifier. The interface can only have a public static final variable.

      - Mukthiyar

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        June 4, 2020

        Yes concept for abstraction is like showing essential things and hiding non essential things and this can be achieved through the Java abstract or interfaces creations.

        - Mukthiyar

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          April 5, 2020

          Your definition of abstraction is definitely that of encapsulation and not of abstraction. Abstraction is a model of real-world objects limited to a specific context where we represent details related to that context with high accuracy and omit the rest! Encapsulation is what you defined abstraction. Thanks for the effort!

          - A. Rouabah

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            March 26, 2020

            I am new in programming, especially OOP. Your article is good. Hope I will be able to master it. Thanks and best wishes.

            - Ayorinde

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              October 27, 2019

              Wow. Just change relapse ‘Abstraction’ with ‘Encapsulation’ and it will be a nice article.

              - John

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                September 30, 2019

                Nice theme and logo sir, Now it’s much better to study any topic

                - Micky

                  Try DigitalOcean for free

                  Click below to sign up and get $200 of credit to try our products over 60 days!

                  Sign up

                  Join the Tech Talk
                  Success! Thank you! Please check your email for further details.

                  Please complete your information!

                  Featured on Community

                  Get our biweekly newsletter

                  Sign up for Infrastructure as a Newsletter.

                  Hollie's Hub for Good

                  Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

                  Become a contributor

                  Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                  Welcome to the developer cloud

                  DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

                  Learn more