The previous three courses were just the beggining. In the last one we started getting somewhere since we could see what is a constructor and what overloading is. A fundamental thing on object oriented programming is called “inheritance”. On real life it is what a parent (on most cases) leaves to a child. In programming it is a bit similar but different.

When creating a class we can define a series of class relationships where one class subclasses, as we call it, another one. This way it can use all the modeling that the mother class has adding it’s own functionality that it will make it unique.

Inheritance Example

But let’s take a look at a real life example. If we want to model the animals we can use the modeling you can see in the image on the right. We can have one class that it is an animal, having properties such as animal name, colour, size etc. Then a mamal can inherit this class, getting all the properties named above, adding a few more such as how many kids it can give birth to. A fish can also inherit the animal adding a few properties such as which sea it is usualy met. If you take a closer look at this modeling you will find it has flaws. A major one is that i cannot model the animal “whale”. This animal is a mamal and also a fish. One would now think we should just add the whale under mamal and fish. This means that the whale will inherit both of them.

This is a major problem. First of all this cannot be done in Java. One class cannot directly inherit more than one class. It can be done in other “object oriented” languages such as C++. Provided that we could do this, we would cause what we call on Object Orientation “A deadly diamond of death”. Let’s see what happens if we add the whale under the mamal and the fish.

Now can you see why we call it a diamond? Well, it is kinda obvious from the shape but why death? Well, the property of the root class “name”, where would the “whale” inherit it from, mamal or fish? This is the problem. That’s the reason why Java does not support inheritance from more than one class. Now, after this i think we should take a look at some code. Here are the files:

Animal.java

public class Animal{
    String name;
    String colour;
    public Animal(String name, String colour){
        this.name = name;
        this.colour = colour;
    }
}

Mamal.java

public class Mamal extends Animal{
    int kids;
    public Animal(String name, String colour, int kids){
        super(name, colour);
        this.kids = kids;
    }
}

Fish.java

public class Fish extends Animal{
    String sea;
    public Animal(String name, String colour, String sea){
        super(name, colour);
        this.sea = sea;
    }
}

Interesting huh? Here are the key points that you should notice on the above code:

  • The keyword “extends”. This actually says that the Mamal and Fish class inherits the Animal class. With this keyword the subclasses get all the non-private properties of the class.
  • The method “super”. This is actually a call to the constructor of the super-class using the parameters in the parenthesis. The super call should be the first one on the subclass constructor. This way we initialize the instance starting from the top.

This is what we need to do to make inheritance happen. We must be very very careful when designing the tree not to bump in problems such as the one i created on my designing of the animal kingdom 🙂

Experiment with classes and subclasses till the next tutorial series and for any problem post back here!