Learn about Abstract Classes in Java

1
2015
Java-Abstract-Classes

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]

[Ship.java]

[Car.java]

[MainClass.java]

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:

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