Home CPSC 330

Inheritance

Composition

So far we've seen a relationship between objects called composition. This is when an object contains another object inside of it, as in this ContactList class.


// a Contact List class
class ContactList {
  // the array now private
  private String [] names;

  // current number we have
  private int count;

  // constructor
  public ContactList() {
    // start with 100 slots
    names = new String[100];
    count = 0;
  }

  // function to get size
  public int size() {
    return count;
  }

  // insert function
  public void insert(String name) {
    // if full, grow
    if(names.length <= count) {
      String[] newNames = new String[count * 2];
      for(int i = 0; i < count; i++) {
        newNames[i] = names[i]; 
      }
      names = newNames;
    }

    names[count] = name;
    count++;
  }

  // get function
  public String get(int i) {
    if(i < names.length) {
      return names[i];
    } else {
      return "ERROR";
    }
  }
}

The ContactList class contains an array of string objects. We say that the ContactList "Has a" array inside of it.


Inheritance

There is another possible relationship between objects which models an "Is a" relationship. For example:

We can model this in our programs using inheritance. When a class inherits from another, it gets a copy of everything in that class.

This is done in Java with the "extends" keyword:


class BaseClass {

}

class DerivedClass extends BaseClass {

}

Here, everything that is in BaseClass would be inherited by DerivedClass objects. We'd say that a DerivedClass object also is a BaseClass object.


Example

Suppose the school wanted to build a record system for storing information about people on campus. They want to store information on students and employees. Some of the information is the same for both:

Whereas some only applies to students: And some only applies to employees:

If we make one class to represent all people, much of the information would be invalid. If we make two separate classes, we will have repetitive information.

This program uses inheritance to solve this problem. The common information is contained in the base class, while unique information is in derived classes.

Notice that we must call the base class's constructor with the super keyword.


protected Attributes

In the example above, the Student information (name & id) are private. Private data cannot be accessed outside of the class. This includes derived classes such as Student and Employee.

Often, we want Derived classes to be able to access some of a Base classes data. Instead of making that data public, we can make it protected.

Protected data can be accessed only by the class itself and derived classes.


Overriding Methods

Inheritance allows us to use another class as a starting point for a new one. We can then add new data and methods to it.

We can also replace methods in the base class by overriding them.

This is done as follows:


class Base {
  public void method() {
    // original
  }
}

class Derived extends Base {
  @Override
  public void method() {
    // new
  }
}

The "@Override" is actually optional, but is good practice.

The base class version of a method can be called from the derived class by prefixing it with "super."

This example shows how we could use overriding in the person information classes.


The Object Class

No class we create is truly a base class in Java. A class that does not extend another one automatically extends the Object class. You can override the methods here including "equals" and "toString".


Inheritance Hierarchies

In Java, each class can only extend from one other class. However, there is no limit on how long a chain of inheritance is.

A deep hierarchy can be confusing and hard to apply effectively, however.


Benefits of Inheritance

Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.