πŸ““9.1: Class Inheritance

Table of Contents


πŸ“– This page is a condensed version of CSAwesome Topic 9.1

Using a GitHub Template for class notes

  1. Go to the public template repository for our class: BWL-CS Java Template
  2. Click the button above the list of files then select Create a new repository
  3. Specify the repository name: CS2-Unit-9-Notes
  4. Click

    Now you have your own personal copy of this starter code that you can always access under the Your repositories section of GitHub!

  5. Now on your repository, click and select the Codespaces tab
  6. Click Create Codespace on main and wait for the environment to load, then you’re ready to code!
  7. πŸ“ Take notes in this Codespace during class, coding along with the instructor.

Class Inheritance

One of the really useful features of Object-Oriented programming is inheritance. You may have heard of someone coming into an inheritance, which often means they were left something from a relative who died. Or, you might hear someone say that they have inherited musical ability from a parent. In Java all classes can inherit attributes (instance variables) and behaviors (methods) from another class. The class being inherited from is called the parent class or superclass. The class that is inheriting is called the child class or subclass.

When one class inherits from another, we can say that it is the same kind of thing as the parent class (the class it inherits from). For example, a car is a kind of vehicle. This is sometimes called the is-a relationship, but more accurately it’s a is-a kind of relationship. A motorcycle is another kind of vehicle. All vehicles have a make, model, and year that they were created. All vehicles can go forward, backward, turn left and turn right.

image

A UML (Unified Modeling Language) class diagram shows classes and the relationships between the classes as seen above. An open triangle points to the parent class.

The parent class for Car and Motorcycle is Vehicle. The Vehicle class has two child classes or subclasses: Car and Motorcycle.

Subclass extends Superclass

To make a subclass inherit from a superclass, use the Java keyword extends with the superclass name when creating a new subclass as shown below.

public class Car extends Vehicle {}
public class Motorcycle extends Vehicle {}

While a human can have two parents, a Java class can only inherit from ONE parent class. If you leave off the extends keyword when you declare a class, then the class will inherit from the Object class that is already defined in Java.

Why Use Inheritance?

Inheritance allows you to reuse data and behavior from the parent class. If you notice that several classes share the same data and/or behavior, you can pull that out into a parent class. This is called generalization.

For example, Customers and Employees are both people, so it makes sense use the general Person class as seen below.

Inheritance is also useful for specialization which is when you want most of the behavior of a parent class, but want to do at least one thing differently and/or add more data. The example below can also be seen as specialization. An employee is a person but also has a unique id. A customer is a person, but also has a credit card.

image

The Student class can also inherit from the class Person just like Employee and Customer because a Student is a type of Person.

  • πŸ’» FIX CODE: What do you need to add to the Student class declaration below to make it inherit from type Person?

    When you fix the code below, the instanceof operator would return true that Student s is an instance of both the Student and the Person class.

public class Person {
    private String name;
}
public class Student {
    private int id;

    public static void main(String[] args) {
        Student s = new Student();
        System.out.println(s instanceof Student);
        System.out.println(s instanceof Person);
    }
}
  • πŸ’¬ DISCUSS: What other private instance variables could you add to Person and Student? In which class would you put an address attribute? Where would you put GPA?

IS-A vs. HAS-A Relationships

is-a Substitution Test

πŸ’» In-Class Activity: Online Store

  1. Go to
  2. Make sure you SIGN IN!
  3. Complete the Programming Challenge: Online Store activity in pairs.

⭐️ Summary

πŸ›‘ When class ends, don’t forget to SAVE YOUR WORK!

  1. Navigate to the Source Control menu on the LEFT sidebar
  2. Type a brief commit message in the box, for example: updated Main.java
  3. Click the button on the LEFT menu
  4. Click the button on the LEFT menu
  5. Finally you can close your Codespace!

Acknowledgement

Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.