July 2002 Technical Tip – An Introduction to Inheritance

Last month we discussed Object Think. First introduced by Coad and Nicola in 1993, Object Think represents a very natural way to think of objects, and from that process we can design the classes which will be used to create those objects. With object think, we let the object talk about itself. Specifically, the object says "This is what I know…" and "This is what I know how to do…" That which I know (or need to know) will become attributes, and that which I know how to do (or need to know how to do) will become behaviors.

This month we introduce a critical component of object-oriented programming – inheritance. In simplest terms, inheritance is an "is a" relationship. In Java, the keyword extends indicates an inheritance relationship. The class which is being extended is the superclass, and the class which is doing the extending is the subclass. Every instance of the subclass "is a" instance of the superclass. We will use a simple example to illustrate this concept.

Consider the following sets of numbers:

  • Integers: …-3, -2, -1, 0, +1, +2, +3, …
  • Whole numbers: 0, +1, +2, +3, …
  • Natural numbers: +1, +2, +3, …

We can illustrate this with a Venn diagram as follows:

In Java we say natural numbers extends whole numbers, and whole numbers extends integers. It is impossible in the limited space available to give this topic the coverage it deserves. The sample programs which follow will work, and the output is shown. Significant functionality is missing (such as getters, setters, error checking and the ability to add, subtract, multiply and divide!) but this should be sufficient to introduce the notion of inheritance to the Java novice.

MyInteger.java

public class MyInteger
{
    // instance variable
    public int value;

    // constructor
    public MyInteger(int aValue)
    {
        value = aValue;
    }

    // toString
    public String toString()
    {
        return "I am an integer whose value is " + value + ".";
    }
}

MyWhole.java

public class MyWhole extends MyInteger
{
    // constructor
    public MyWhole(int aValue)
    {
        super(aValue);
    }

    // toString
    public String toString()
    {
        return super.toString() + "\n" +
           "I am also a whole number.";
    }
}

MyNatural.java

public class MyNatural extends MyWhole
{
    // constructor
    public MyNatural(int aValue)
    {
        super(aValue);
    }

    // toString
    public String toString()
    {
        return super.toString() + "\n" +
          "I am also a natural number.";
    }
}

TestMyNumbers.java

public class TestMyNumbers
{
    public static void main(String args[])
    {
        MyInteger i = new MyInteger(-3);
        System.out.println(i + "\n");

        MyWhole w = new MyWhole(0);
        System.out.println(w + "\n");

        MyNatural n = new MyNatural(+4);
        System.out.println(n + "\n");
    }
}

Actual Output

C:\Java>java TestMyNumbers
I am an integer whose value is –3.

I am an integer whose value is 0.
I am also a whole number.

I am an integer whose value is 4.
I am also a whole number.
I am also a natural number.

Key Points

  • Note the use of the extends keyword in the MyWhole and MyNatural classes, indicating inheritance.
  • Note the use of the super keyword in the constructors for the MyWhole and MyNatural classes. To create an instance of MyNatural, one must create an instance of MyWhole, and to create an instance of MyWhole, one must create an instance of MyInteger. The super keyword within the constructor does this.
  • Note the use of the super keyword in the toString methods of the MyWhole and MyNatural classes. In each case, we return the result of the toString method from the superclass with another message appended to that indicating the nature of the subclass.

Closing Comment

This tip is presented with a working application. You'll find that our classes – like our tips – are loaded with examples, making for a more enjoyable and productive learning experience. We hope you will consider Caliber Data Training for your training and development needs!


Go to the articles index. Written by Bill Qualls. Copyright © 2002 by Caliber Data Training 800.938.1222