Inheritance

Inheritance is the object-oriented concept of an object class being able to “inherit” another classes attributes and methods. This is useful where you have a number of objects in a program that share the same attributes and methods.

An example could be a pet store program that stores information about various different animals. We may need classes for a Cat, Dog and Bird. These animals may have simliar attributes that we could put in one class that all the various animal object classes could use. We could call this class Animal.java and this would be the master class which is called a super class. This will be the class that our Cat, Dog and Bird classes will inherit from. Common attributes could be the animal type, life expectancy, diet description and weight.

Our Animal class could look as follows:

public class Animal
{
    private String animalType = "";
    private int lifeExpect = 0;
    private String diet = "";
    private double weight = 0.0;

    // default constructor
    public Animal ()
    {

    }

    // parameterised constructor
    public Animal (String animalTypeIn, int lifeExpectIn, String dietIn, double weightIn)
    {
        setAnimalType (animalTypeIn);
        setLifeExpectancy (lifeExpectIn);
        setDiet (dietIn);
        setWeight (weightIn);
    }

    public void setAnimalType (String animalTypeIn)
    {
        animalType = animalTypeIn;
    }

    public void setLifeExpectancy (int lifeExpectIn)
    {
        lifeExpect = lifeExpectIn;
    }

    public void setDiet (String dietIn)
    {
        diet = dietIn;
    }

    public void setWeight (double weightIn)
    {
        weight = weightIn;
    }

    public String getAnimalType ()
    {
        return animalType;
    }

    public int getLifeExpectancy ()
    {
        return lifeExpect;
    }

    public String getDiet ()
    {
        return diet;
    }

    public double getWeight ()
    {
        return weight;
    }

    public String toString ()
    {
        return "Animal Type" + getAnimalType () +
            " Life Expectancy: " + getLifeExpectancy ()
            + " Diet: " + getDiet () + " Weight: " + getWeight ();
    }
}

We can now create our Cat, Dog and Bird classes to make use of the common attributes and methods in the Animal class. In Java we use the keyword extends to show that we want to inherit a particular class. In this case our first class Cat.java will extend the Animal.java class.

Our Cat class could look as follows:

public class Cat extends Animal
{
    private String furColour = "";
    private String breed = "";

    public Cat ()
    {

    }

    public Cat (int lifeExpectIn, String dietIn, double weightIn, String furColourIn, String breedIn)
    {
        super ("Cat", lifeExpectIn, dietIn, weightIn);
        setFurColour (furColour);
        setBreed (breed);
    }

    public void setFurColour (String furColourIn)
    {
        furColour = furColourIn;
    }

    public void setBreed (String breedIn)
    {
        breed = breedIn;
    }

    public String getFurColour ()
    {
        return furColour;
    }

    public String getBreed ()
    {
        return breed;
    }

    public String toString ()
    {
        return super.toString () + " Fur Colour: " + getFurColour () + " Breed: " + getBreed ();
    }
}

The first thing to notice is the class definition public class Cat extends Animal. This means that the Cat class can make use of the methods that are in the Animal class as if they were actually written inside the Cat class. The class that has been extended is referred to as the super class. The super keyword is therefore used to reference methods in the super class.

In the parameterised constructor of the Cat class we make use of the super keyword to call the Animal class constructor. This means that the parameterised constructor in the super class (Animal) must be used to handle the parameters that are for attributes in the Animal class. We then use the set and get methods for the additional attributes that have been created in the Cat class.

In the toString() method of the Cat class we can see that super.toString() has been used to call the toString() method from the Animal class. The additional attributes of the Cat class are then added and returned.

In the same way we can create Dog and Bird classes which extend the Animal class. Additional attributes could then be added that are specific to dogs and birds in each class.


2 comments on “Inheritance
  1. fiam says:

    Good info. Lucky me I recently found your blog by chance
    (stumbleupon). I’ve book marked it for later!

  2. Website says:

    This is my first time visit at here and i am actually pleassant to read all at alone place.|

Leave a Reply

Your email address will not be published. Required fields are marked *

*