By Tony Bevis on Sunday, 08 October 2023
Category: Java

Java programming course: 3.5 Abstract classes and methods

In the previous lesson you learnt about overriding.

Abstract classes and methods

Now that you have created three subclasses of Animal you might like to consider what is meant by "animal": does it ever make sense to instantiate an Animal object that isn't a specific type of animal? It seems natural to think that "animal" is in fact an abstract concept. In real-world terms, every animal in existence is a specific type of animal, and it is not possible for any animal to be born without it being a specific type.

Java lets you model this concept by marking classes as abstract. Once you do this, it is no longer possible to instantiate an object of type Animal unless you specify its actual type. Change the class declaration within Animal as follows:

If within VirtualZoo you still have lines which instantiate using new Animal, you will find they now give rise to an error message:

You have now successfully prevented client objects from creating an Animal object without specifying which specific type of animal it is.

It is often the case with abstract classes that there is some functionality which will differ for each subclass but which has no sensible default. An example for animals would be their favourite food; unlike the isEndangered() method where it was assumed that the animal was not endangered unless specifically overridden, there is no obvious foodstuff that can serve as a sensible default.

With abstract classes this presents no problem; simply define an abstract method. Insert the following inside the Animal class:

Note the following:


In the Lion class, override this method by inserting the following:

In the Penguin class, override this method by inserting the following:

In the Monkey class, override this method by inserting the following:

In the next lesson we will tidy up the Animal constructors.

Lesson 3.6 Tidying up the Animal constructors 

Related Posts

Leave Comments