Inheritance: 9.1.1

  • inheritance is a feature of Object-Oriented Programming
    • All classes can inherit instance variables or methods from another class
      • The class being inhereted from is the parent class or superclass
      • This class becomes the child class or subclass of the parent
      • A class can only have one parent
  • A child class is the same kind of thing as a parent
    • A car is a kind of vehicle
      • Vehicle is the parent class, Car is the child
  • All classes inherit from java.lang.Object

UML

  • A UML (Unified Modeling Language) class diagram shows classes and relationshis between classes
  • An open triangle points to the parent class

A UML Diagram of inheritance

Subclass extends Superclass: 9.1.2

  • To make a subclass inheret from a superclass, use the keyword extends
public class Car extends Vehicle
public class Motorcycle extends Vehicle

Why Use Inheritance?: 9.1.3

  • Inheritance allows you to reuse data and behavior from a parent class
    • Generalization
      • Generalize many classes into the same “umbrella”
  • It is useful for specialization
    • You want most of the parent behavior
      • You want to do a few things differently

Test for inheritance

  • You can test for inheritance by using the instanceof operator
public class Person {}  // stub code

public class Student extends Person {} // stub code

Person p = new Person("John");
Student s = new Student("James");

if (s instanceof Person) {
    // this will run; is true!
}

if (p instanceof Student) {
    // this will not run; is false!
}

is-a vs. has-a: 9.1.4

  • Another type of relationship is the has-a relationship
    • association relationship
    • This is when a class has a reference to another class

UML

A UML Diagram for association

  • The 1 near Course means there is 1 course object associated with the number on the other end of the line
    • This is an asterisk: 0 to many
    • There is one Course associated with 0-many CoursePeriods

Java

  • The Course class has an array/list of CoursePeriod objects as an attribute
  • A CoursePeriod has a Course attribute to hold the information about the course
import java.util.ArrayList;

public class CoursePeriod {
    private Course courseInfo;
    private int period;
}

public class Course {
    private ArrayList<CoursePeriod> periodList;
}

is-a Substitution Test: 9.1.5

  • If you aren’t sure if a class should inherit another, try to see if you can substitute the subclass type for the superclass
    • If it doesn’t make sense, use association

Summary: 9.1.7

  • A class heirarchy can be developed by putting common attributes and behaviors of related classes into a superclass
  • Classes that extend a superclass, called subclasses, can use existing attributes in a superclass without repeating these in code
  • The keyword extends is used to establish inheritance between a subclass and a superclass
    • A class can only extend one superclass
  • Extending a subclass from a superclass creates an is-a relationship from the subclass to the superclass