OOPs principle – Abstraction via interface3 min read
What is Abstraction?
Google’s definition – the state of thinking deeply about something and not paying attention to what is around you.
Main idea behind the abstraction is to hide all the unnecessary details from the user and by doing this, we enable the user to implement more complex ideas/logics on top of the exposed abstraction without even thinking about the hidden implementation or complexity in the implementation.
Simple real world abstraction example
A simple real-world example is always a good way to capture/understand things quickly and easily. And lets take the same good old example of Car.
- Start engine (Give user a button, but hide the complexity behind the scene)
- Speed up (Give user a Gear/Lever to speed up the car, but hide the complexity behind the scene).
- Speed Down (Give user a Gear/Lever to speed down the car, but hide the complexity behind the scene).
- Stop engine (Give user a button to stop the car)
The thing you don’t need to know is how the car is working internally to speed up or speed down. You don’t need to know the engine capacity or how much torque or power or is it disk or drum brake system and the list just goes on with all unknown details which are inside the car.
So, how we interact the car? By using a simple interface. In the same way, we are going to expose interface which doesn’t require any understanding about the internal implementation.
Lets put this example in action:
package com.zingscoop.abstraction;
public interface Car {
public void startEngine();
public void stopEngine();
public void speedUp();
public void speedDown();
public String carType();
}
Lets create couple of classes which implements the functionalities of the Car interface.
package com.zingscoop.abstraction;
public class ElectricCar implements Car{
private String carType;
public ElectricCar(String carType) {
this.carType = carType;
}
@Override
public void startEngine() {
System.out.println("Start the electric car");
}
@Override
public void stopEngine() {
System.out.println("Stop the electric car");
}
@Override
public void speedUp() {
System.out.println("Speed up the Electric Car");
}
@Override
public void speedDown() {
System.out.println("Speed down the Electric Car");
}
@Override
public String carType() {
return this.carType;
}
}
package com.zingscoop.abstraction;
public class DieselCar implements Car {
private String carType;
public DieselCar(String carType) {
this.carType = carType;
}
@Override
public void startEngine() {
System.out.println("Start the Diesel car ");
}
@Override
public void stopEngine() {
System.out.println("Stop the Diesel car ");
}
@Override
public void speedUp() {
System.out.println("Speed up the Diesel Car");
}
@Override
public void speedDown() {
System.out.println("Speed down the Diesel Car");
}
@Override
public String carType() {
return this.carType;
}
}
Finally lets create a main app class which can initialise the object and use it without having knowledge of the implementations.
import com.zingscoop.abstraction.Car;
import com.zingscoop.abstraction.DieselCar;
import com.zingscoop.abstraction.ElectricCar;
public class App {
public static void main(String[] args) throws Exception {
Car dieselCar = new DieselCar("DieselCar");
dieselCar.startEngine();
dieselCar.speedUp();
dieselCar.speedDown();
dieselCar.stopEngine();
System.out.println("========= ZingScoop ======");
Car electricCar = new ElectricCar("ElectricCar");
electricCar.startEngine();
electricCar.speedUp();
electricCar.speedDown();
electricCar.stopEngine();
}
}
Lets run the main app and see the output
Start the Diesel car
Speed up the Diesel Car
Speed down the Diesel Car
Stop the Diesel car
========= ZingScoop ======
Start the electric car
Speed up the Electric Car
Speed down the Electric Car
Stop the electric car
This is one of the implementation of abstraction by using the interface. We can achieve the abstraction by using the abstract class which is covered here ( Abstraction via abstract class)
All the above examples are available in github
Stay subscribed. more to come. Happy Scooping…