Java ProgrammingLearn about Abstract Classes in Java

Learn about Abstract Classes in Java

Java-Abstract-Classes

An abstract class is a class that you cannot instantiate; you must extend it in order to use it. Only the methods that you extend, will be available in the child class. But why is this useful? Because sometimes you want to ensure that a class, or a set of classes, must implement as set of methods and must have access to a set of properties. For example, you may create an abstract class called Vehicle, which will be the basis of any child class that describes a vehicle (cars, tanks, ships…etc.). This class ensures that every child class will have a drive(), stop(), and park() methods. It’s up to those classes to provide their own implementation of those methods, hence they will be created with empty bodies. An abstract class can also provide normal methods that may or may not be overridden. In the example below, we are going to define an abstract class that defines and implements a horn() method, which will be available to all children classes, and they will not be forced to implement it. Let’s have a look at the example:

[Vehicle.java]

public abstract class Vehicle {
    private String _color;
    private String _model;
    
    Vehicle(String color, String model){
        this._color = color;
        this._model = model;
    }
    
    abstract void drive();
    abstract void stop();
    abstract void park();
    
    void horn(){
        System.out.println("Peeeeeep");
    }
}

[Ship.java]

public class Ship extends Vehicle {

    public Ship(String color, String model) {
        super(color, model);
    }

    @Override
    void drive() {
        System.out.println("Ship is moving");
    }

    @Override
    void stop() {
        System.out.println("Ship is stopping");
    }

    @Override
    void park() {
        System.out.println("Ship is docking");
    }
    
}

[Car.java]

public class Car extends Vehicle {

    private int _doors;
    
    public Car(String color, String model, int doors) {
        super(color, model);
        this._doors = doors;
    }

    @Override
    void drive() {
        System.out.println("Car is in drive mode");
    }

    @Override
    void stop() {
        System.out.println("Car is in stop mode");
    }

    @Override
    void park() {
        System.out.println("Car is in park mode");
    }
}

[MainClass.java]

public class MainClass {

    public static void main(String[] args) {
       Car sportsCar = new Car("White","McLaren",2);
       sportsCar.drive();
       sportsCar.stop();
       sportsCar.park();
       sportsCar.horn();
       
       Ship speedBoat = new Ship("Green","Riva");
       speedBoat.drive();
       speedBoat.park();
       speedBoat.stop();
       speedBoat.horn();
    }
}

In Vehicle.java, we are defining the abstract class by adding the abstract keyword before the class name. Inside it, we have two private fields, and a default constructor that initializes them. Then, we define three abstract methods, denoted by the keyword abstract before the method name. As mentioned, abstract methods have no body. Then we define a normal, non-abstract method horn().

Java Programming Course for Beginner From Scratch

In the Car.java, we created a class Car that extends the abstract class Vehicle. And because it did, it must implement all the abstract methods of this class. However, it needn’t override the horn() method because it was not defined as abstract. In fact every vehicle would probably have a horn, and “Peeeeep” seems to be a good representation of a horn being “honked” so we won’t override it. We also added a new field that is specific to cars: the number of doors, and added it to the initialization logic of the class in the constructor method.

Then we created another class, Ship, which extended the Vehicle class. We defined the ship-implementation of the abstract methods.

Calling the respective methods of Car and Ship will provide the following output:

Car is in drive mode
Car is in stop mode
Car is in park mode
Peeeeeep
Ship is moving
Ship is docking
Ship is stopping
Peeeeeep

So, what difference could be made if we defined the Vehicle class as a normal class and extended it? Effectively, nothing. However, using abstract classes and methods gives you more control as you force the user of your code to provide a specific implementation for a very generic entity (like a vehicle).

Conclusion

In this article, we introduced the concept of abstract classes. You have learned what are they and how they could be used to force the client code to provide a specific implementation of a rather generic entity.

In the next article, we are going to visit interfaces, which may look like abstract classes at first but, trust me, they are a lot different and have their own usage scenarios in the object oriented programming world. I hope you enjoyed reading this part. See you next time.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -