Java Lesson 4 – Abstraction

Up until now we have been looking at creating pure classes that have one singular purpose and functionality. However, it may be that some classes share very similar functions and fields. In this case it may be useful to use abstraction and inheritance.

Abstraction allows us to declare an uninstantiable class to act as a parent to sub-classes. For instance, we could have an abstract class “Vechicle” and from that class the sub-classes “Car”, “Lorry” and “Motorbike” could then inherit.

The hierarchy could become even more advanced by having further classes inherit from those sub-classes. Below is a class diagram to show how the inheritance works.

To create an abstract class we must first create a class like normal, then insert the keyword “abstract” in the class declaration:

Whatever we declare in this class will be universal to all classes that inherit from it. For the vehicle example, we’ll include wheels, seats and top speed.

Now we can create a class that inherits from the abstract class, let’s take a look at the Car and Lorry example.

Let’s say Car and Lorry share the same field “numHeadlights”. We could encapsulate this into a new class “MotorisedVehicle” and the Car and Lorry can inherit from this instead:

Then we can implement manual vehicles, and a class to inherit from that:

Access modifiers are again important, private variables in the abstract class will not be visible to the sub-classes, so bear this in mind when creating your classes.

There are many uses for abstract classes, one of those we’ve seen here. But another good use for abstract classes is to allow the loading of external class files at run time. If your application runs classes that inherit from an abstract class, it could then be possible for other uses to write classes that inherit from it too. These new classes could then be used at runtime using something called “Reflection”.

We won’t cover reflection just yet, but it is something important to bear in mind for allowing users to create plugins and files for use in your future applications.

Interfaces are another interesting type of Java class, we’ll be looking at them next time, but do keep abstract classes in mind as we go through them.

Abstraction and Inheritance are both key elements of object-oriented programming and design, the idea that you can minimise the amount of code you have to write by organising your classes into a hierarchy has been an interesting area of programming since its inception. The concepts are pretty much commonplace nowadays and if you are unfamiliar with the concepts behind object-oriented design, I would strongly advise you go and research the area yourself.

Next Time

Next time we will be covering interfaces as we said earlier, and enumerators. Both of these are other types of Java class in a way and offer us interesting features to use in our applications.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started