create an inheritance relationship from a subclass to the superclass
One of the really useful features of Object-Oriented programming is inheritance.
You may have heard of someone coming into inheritance, which often means they were left something from a relative who has passed away.
Or, you might hear someone say that they have inherited their 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.
A motorcycle is another kind of vehicle that Mr. C enjoys riding when the weather is warm.
All vehicles have a make, model, and a year they were created (attributes/instance variables).
All vehicles can go forward, backward, turn left and turn right (methods/behaviors)
A UML (Unified Modeling Language) class diagram shows classes and the relationships between the classes as shown in Figure 1 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
Note: While a person 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 automatically 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 to use the general Person class 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 above 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 credit card information.
In the following example, the Student class can inherit from the class Person because a Student is a type of Person:
Notice that we needed to use the extends keyword for the Student class declaration to make it inherit from the Person class.
The instanceof operator returns true to show that Student s is an instance of both the Student and Parent class.
More private instance variables could easily be added to this program, but it is important to consider which class to put them in!
For example, I would put an address attribute in the Person class (because every person has an address) and GPA in the Student class (because only students have a GPA).
IS-A VS. HAS-A
Another type of relationship between classes is the has-a relationship or association relationship.
Use this when the object of one class contains a reference to one or more of another class.
For example, a course can have many course periods associated with it as shown below:
The 1 near the Course means that 1 course object is associated with the number shown near the other class.
In this case it is * which means 0 to many.
So one course is associated with 0 to many course periods.
In the code, the Course class has an array or ArrayList of CoursePeriod objects as an attribute:
Alternatively, we could say that a CoursePeriod has a Course attribute inside it to hold the information about the Course.
It is up to the programmer how to design these two classes depending on which type of association would be more useful in the program.
Consider the classes Student, Course, and APcourse.
An APcourse is a special type of Course.
Students are in Courses.
What are the relationships between these classes?
The UML diagram below shows the inherits (is-a) relationship between Course and APcourse and the associate (has-a) relationship between Course and Students:
We can represent the diagram in Figure 4 with the code below.
The Course class has an ArrayList of Student objects in it as the roster attribute.
And an APcourse extends Course.
IS-A SUBSTITUTION
If you aren't sure if a class should inherit from another class ask yourself if you can substitute the subclass type for the superclass type.
For example, if you have a Book class and it has a subclass of ComicBook does that make sense?
Is a comic book a kind of book?
Yes, a comic book is a kind of book so inheritance makes sense.
If it doesn't make sense use association or the has-a relationship instead.
SUMMARY
A class hierarchy can be developed by putting common attributes and behaviors of related classes into a single class called a superclass.
Classes that extend a superclass, called subclasses, can draw upon the existing attributes and behaviors of the superclass without repeating these in the code.
The keyword extends is used to establish an inheritance relationship between a subclass and a superclass. A class can extend only one superclass.
Extending a subclass from a superclass creates an is-a relationship from the subclass to the superclass.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) Remember, you are welcome to use pair programming (no groups of three). Design an online store with classes for Store, ItemForSale, Book, Movie, and Author.
First, I recommend doing some research in an online store like Amazon to see what information they store on books, movies, and authors, and what type of information is the same for all items for sale.
There should be at least 2 attributes (instance variables) for each class.
There are relationships between theses classes, for example, between ItemForSale and Movie, Book and Author, and Store and ItemForSale. You may want to draw UML Class Diagrams for these classes.
So, for this project, make sure to declare each class and specify their relationship to one another with inheritance or association. The classes only need to contain the instance variables (at least 2) for each class, we will learn how to add constructors and methods in the next lessons.