Tutorials  /  Linux Basics

What is Abstraction in OOPS?

Ccentron Redaktion · January 2024 ·3 min read ·Linux Basics, Tutorial

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. It is used to describe things in simple terms and create a boundary between the application and the client programs.

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

2. Abstraction in Real Life

Abstraction is present in almost all the real-life machines. Your car and microwave are great examples of abstraction where the complex logic and internal implementation are completely hidden from the user, providing a simple interface to interact with.

3. Abstraction in OOPS

Objects are the building blocks of Object-Oriented Programming. An object contains some properties and methods which we can hide from the outer world through access modifiers, providing access only to the required functions and properties to 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: Data Abstraction and Process Abstraction. Data Abstraction involves hiding the object data from the outer world, providing access through methods if needed. Process Abstraction hides the internal implementation of the different functions involved in a user operation.

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

Go
package com.journaldev.oops.abstraction;

public interface Car {

	void turnOnCar();

	void turnOffCar();

	String getCarType();
}

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

Go
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.

Go
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

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren