Abstraction
In object-oriented programming, abstraction is a technique of hiding internal implementation details and showing only the functionality. Abstract classes and interfaces are used to achieve abstraction in Java.
A real-world example of car abstraction is that internal details such as the engine, the process of starting the car, and the process of changing gears, etc., are hidden from the user, while features like the start button, gear box, display, braking system, and so on are provided to the user. When we perform any action on these features, the internal processes operate automatically.
Source Code: Polymorphism.java
// Abstract class abstract class Vehicle { // Abstract methods abstract void start(); abstract void stop(); // Concrete method void fuelType() { System.out.println("Uses petrol or diesel."); } } // Subclass 1 class Car extends Vehicle { @Override void start() { System.out.println("Car is starting with a key."); } @Override void stop() { System.out.println("Car is stopping using brakes."); } } // Subclass 2 class Bike extends Vehicle { @Override void start() { System.out.println("Bike is starting with a kick or button."); } @Override void stop() { System.out.println("Bike is stopping using hand brakes."); } } public class VehicleCar { public static void main(String[] args) { Vehicle myCar = new Car(); myCar.start(); // Output: Car is starting with a key. myCar.stop(); // Output: Car is stopping using brakes. myCar.fuelType(); // Output: Uses petrol or diesel. System.out.println(); Vehicle myBike = new Bike(); myBike.start(); // Output: Bike is starting with a kick or button. myBike.stop(); // Output: Bike is stopping using hand brakes. myBike.fuelType(); // Output: Uses petrol or diesel. } }
Result:
Watch the video:

