Abstraction exposes to the user only those things that are relevant to them and hides the remainder of the details. In OOP terms, we say that an object should expose to the users only a set of high-level; operations, while the internal implementation of those operations is hidden.
Abstraction is achieved in Java via abstract classes and interfaces
Java Abstraction Example via Interface
Let's consider a real-life example: a man driving a car. The man knows what each pedal does and what the steering wheel does, but he doesn't know how these things are done internally by the car. He doesn't know about the inner mechanisms that empower these things. This is known as abstraction is.
Let's create a Car interface with high-level methods:
public interface Car {
public void speedUp();
public void slowDown();
public void turnRight();
public void turnLeft();
public String getCarType();
}
Next, each type of car should implement the Car interface and override these methods to provide the implementation of these methods. This implementation is hidden from the user.
For example, here is the ElectricCar class implementation:
public class ElectricCar implements Car {
private final String carType;
public ElectricCar(String carType) {
this.carType = carType;
}
@Override
public void speedUp() {
System.out.println("Speed up the electric car");
}
@Override
public void slowDown() {
System.out.println("Slow down the electric car");
}
@Override
public void turnRight() {
System.out.println("Turn right the electric car");
}
@Override
public void turnLeft() {
System.out.println("Turn left the electric car");
}
@Override
public String getCarType() {
return this.carType;
}
}
The user of this class has access to these public methods without being aware of the implementation:
public class Main {
public static void main(String[] args) {
Car electricCar = new ElectricCar("BMW");
System.out.println("Driving the electric car: " + electricCar.getCarType() + "\n");
electricCar.speedUp();
electricCar.turnLeft();
electricCar.slowDown();
}
}
Output:
Driving the electric car: BMW
Speed up the electric car
Turn left the electric car
Slow down the electric car
So, this was an example of abstraction via an interface.
Java Abstraction Example via Abstract Class
Let's create an abstract class Car with the following code in it:
abstract class Car {
private final String carType;
// abstract class can have constructor
public Car(String carType) {
this.carType = carType;
}
// these are abstract methods
abstract void speedUp();
abstract void slowDown();
abstract void turnRight();
abstract void turnLeft();
// this is a concrete method
public String getCarType() {
return carType;
}
}
Next, each type of car should implement the Car abstract class and override these methods to provide the implementation of these methods. This implementation is hidden from the user.
For example, here is the ElectricCar class implementation:
public class ElectricCar extends Car {
public ElectricCar(String carType) {
super(carType);
}
@Override
public void speedUp() {
System.out.println("Speed up the electric car");
}
@Override
public void slowDown() {
System.out.println("Slow down the electric car");
}
@Override
public void turnRight() {
System.out.println("Turn right the electric car");
}
@Override
public void turnLeft() {
System.out.println("Turn left the electric car");
}
}
The user of this class has access to these public methods without being aware of the implementation:
public class Main {
public static void main(String[] args) {
Car electricCar = new ElectricCar("BMW");
System.out.println("Driving the electric car: " + electricCar.getCarType() + "\n");
electricCar.speedUp();
electricCar.turnLeft();
electricCar.slowDown();
}
}
Output:
Driving the electric car: BMW
Speed up the electric car
Turn left the electric car
Slow down the electric car
Related Java OOPS Examples
- Java Abstraction Example
- Java Inheritance Example
- Java Encapsulation Example
- Java Simple Inheritance Example
- Java Composition Example
- Java Aggregation Example
- Java Delegation Example
- Java Method Overloading Example
- Java Method Overriding Example
- Java Single Inheritance Example
- Java Multilevel Inheritance Example
- Java Hierarchical Inheritance Example
- Java Abstract Class Example
- Java Class Example