Inheritance, Superclass, Subclass
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
orsuperclass
- This class becomes the
child class
orsubclass
of the parent - A class can only have one parent
- The class being inhereted from is the
- All classes can
- 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
- A car is a kind of vehicle
- 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
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”
- Generalization
- It is useful for
specialization
- You want most of the parent behavior
- You want to do a few things differently
- You want most of the parent behavior
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
relationshipassociation
relationship- This is when a class has a reference to another class
UML
- 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-manyCoursePeriod
s
Java
- The
Course
class has an array/list ofCoursePeriod
objects as an attribute - A
CoursePeriod
has aCourse
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
- If it doesn’t make sense, use
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 establishinheritance
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
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.