Inheritance

Inheritance

Unfortunately in Java, this doesn't mean you'll be receiving vast sums of money. It has everything to do with relationships.

I've already mentioned the “is a” and “has a” relationships in the last episode, but here's a quick review:

Inheritance is all about the “is a” relationship. The example I'll use revolves around the following Objects:

Vehicle
Car
Bus
Motorcycle

With these objects, we identify the “is a” relationships by saying this out loud: A Car is a Vehicle, a Bus is a Vehicle, a Motorcyle is a Vehicle. Since all three of those statements are TRUE, then that means we've got inheritance. Car, Bus and Motorcycle will INHERIT the base functionalities that the Vehicle class defines.

Okay, so what does that mean? Well, we first have to define what functionality a Vehicle has… so what can you think of as a common functionality between all vehicles… all modes of transportation? Well, they should all have wheels of some kind right? What else? They should all drive in some way, take you from point A to point B… they probably all use some kind of fossil fuel. These are all the common things between Vehicles.

Now that we've got an idea of what ALL vehicles have in common, now I need to address the topic of inheritance. So let's say that the Vehicle class has some methods that define this global functionality. The Vehicle class could have a drive() method, a getFuelType() method, and a getNumberOfWheels() method. Since the Vehicle class defines these methods, they will be inherited by its child classes: Car, Bus and Motorcycle. So now all three of the Objects will be able to drive, identify their types of fuel and how many tires they have.

Think of it like this, when the vice president of your company has a request, they pass it along to the directors, each director gets in touch their the managers that work under them, who in turn hand over the assignment to you, the worker. This is inheritance at its finest. A hierarchy of “crap rolling downhill”.

But here's the twist, in Java there are a couple ways of implementing inheritance. This involves the use of two keywords, implements and extends.

Let's talk about the implements keyword. When using the implements keyword, we're talking about interfaces. You can think of an interface like a contract. This contract is what defines how any given object should work if they were to implement the interface. Interfaces are useful when you're designing a system. This is true because with interfaces, you're actually NOT ALLOWED to define any sort of functionality inside of them. All you can do is outline which methods or variables should be used. It's kind of like writing an outline for an essay, you jot out your point form notes and then fill in the specific details later. That's what interfaces are all about. Another thing to note about interfaces, is that any Object in Java is allowed to implement as many interfaces as they like and also an interface is not allowed to be instantiated. One loose rule of thumb when thinking of creating an interface is if you are creating an Object that ends with “able”. For example, there are two interfaces that are built into Java: Cloneable and Runnable. These two interfaces outline the contract that needs to be fulfilled if you wish to ensure that your Object can be cloned, or run on a separate thread. So with our example, we could define a Driveable interface. This interface could outline the drive() method. This would mean that if an Object were to implement our Driveable interface, then that Object would HAVE to define it's own drive() method. So like I said, it's all about the contract.

Now, another form of inheritance as I mentioned earlier was around the keyword extends. In contrast to the interface, when you use the extends keyword you are allowed to define actual functionality. This can be done be either using a regular class, or an abstract class. So in general, any class can extend another class, BUT, it can only extend ONE class. So the extends keyword is really where this sense of hierarchy comes from. The interface just defines a contract, but doesn't really outline a hierarchy, but using the extends keyword does. So when we extend a class, we will inherit all of the super classes functionality, as well as be able to outline even more and specific functionality in the child class. In our example, Vehicle would be the super class, and Car/Bus/Motorcycle would be the child classes.

A good real example would be if our Vehicle class defined the getFuelType() method. We could make this method just return a String which defines the type of fuel a Vehicle would use. We could just put in that a vehicle would use “Petrol” as a fuel. So then, by default all of the child classes would then use Petrol. But what if we know that one particular type of Vehicle doesn't use Petrol, like say, a Bus. Well, then we could use something called Polymorphism to override this functionality. We could redefine this getFuelType() on the Bus object and have it instead return “Diesel”. Now I also mentioned abstract classes, what're those? Well an abstract class is a class that actually is not allowed to be instantiated, just like the interface. This means that if you defined the Vehicle class as abstract, then you would not be allowed to write:

Vehicle aVehicle = new Vehicle();

The abstract class has some similarities to an interface, because with an abstract class you can declare abstract methods, which are methods that don't have any code inside them… it's just defining the contract for that one particular method. But with an abstract class, you CAN define actual functionality if you wish, so you can sort of have the best of both worlds (except that you cannot extend multiple abstract classes, you can still only extend one per Object). Note that if you create an abstract class with nothing but abstract methods inside of it, then you should probably be using an interface.

Homework

Read through this article to help cement what you've just learned about inheritance and follow through the coding examples: https://www.coderscampus.com/java-inheritance/

Once you've got a good idea of how inheritance works, try your hand at this practice assignment: https://www.coderscampus.com/java-practice-assignment-2/

Free Java Beginners Course

Start learning how to code today with our free beginners course. Learn more.