Demonstrate abstract class & abstract method

Q. Write a Java Program to demonstrate the abstract class and abstract method.

Answer:

Abstraction:
Abstraction is the technique of hiding the implementation details and showing only functionality to the user.

In this example, Vehicle is the abstract class and getSpeed is the abstract method. The abstract methods don’t have implementation details.

Vehicle.class

abstract class Vehicle
{    
    abstract int getSpeed();    
}

  

Bike.java

class Bike extends Vehicle
{    
        int getSpeed()
        {
                return 60;
        }    
}


Car.java

class Car extends Vehicle
{    
        int getSpeed()
        {
                return 70;
        }    
}


TestVehicle.java

public class TestVehicle
{    
        public static void main(String args[])
        {    
                Vehicle bike = new Bike();
                Vehicle car = new Car();
                int bikespeed = bike.getSpeed();    
                int carspeed = car.getSpeed();
                System.out.println("Speed of Bike is: "+bikespeed+" Km/h");
                System.out.println("Speed of Car is: "+carspeed+" Km/h");
    }
}


Output:

abstract class method