OOPs principle – Abstraction by Abstract class3 min read
Before we start on the abstraction by Abstract class, I would highly recommend you to go through the Abstraction by Interface where we have covered the explanation of abstraction and how it behaves.
Now, this is an extension of the same example which we covered in the Abstraction by Interface example.
Lets get started by creating a same Car class but it will be an abstract class, not an interface.
package com.zingscoop.abstractionViaAbstractClass;
public abstract class Car {
private final String carType;
public String getCarType() {
return carType;
}
// this constructor will be called from the implementation classes by using super keyword
public Car(String carType) {
this.carType = carType;
}
abstract void startEngine();
abstract void stopEngine();
abstract void speedUp();
abstract void speedDown();
}
If you can compare the above Car class with Car class in here. You can see that we just added only car type as new variable and marked the class and methods as abstract. Now the car type will directly flow from the client side to the abstract class and the implementation class like DieselCar and ElectricCar will be just an passing by for the car type with the help of the constructor (by using super keyword).
package com.zingscoop.abstractionViaAbstractClass;
public class DieselCar extends Car {
public DieselCar(String carType) {
super(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");
}
}
package com.zingscoop.abstractionViaAbstractClass;
public class ElectricCar extends Car {
public ElectricCar(String carType) {
super(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");
}
}
Now lets simulate the car by using an sample app as below
package com.zingscoop.abstractionViaAbstractClass;
public class AbstractionViaAbstractClassApp {
public static void main(String[] args) {
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();
}
}
Now when we run the main app, you will notice the same output which we received in this example.
stractClass.AbstractionViaAbstractClassApp
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
To summarise things shortly,
- Abstraction is a way to hide all the complex implementation headache from the user.
- User can simple make use of the simple methods which is exposed to make use of the underlying complex implementation.
- Abstraction can be achieved in two ways. ( by Interface or by Abstract class )
You might be wondering whats the difference b/w interface and abstract class implementation? right? Then give yourself a pat at the back because it was a great question. And in the next post, I will cover these topic. (like when we should use abstract class and when we should use interface for our class design).
All the above examples are available in github
Happy Scooping. Do share, if you like it. 🙂