Lesson 23 – Inheritance

When you come in
- Connect to the Internet
- Log in to Piazza
- Start Bluej
- Navigate to laughton.com/obrien/sjsu/cs46a/CRT_lessons/ and open this lesson
Subclasses and super classes
- Super classes are more general (BankAccount)
- Sub classes are more specific (CheckingAccount, SavingsAccount)
- A subclass can do everything its super class can do, plus more
- Think of two possible classes where one would be the super class and the other the sub class
Activity - Inheritance Heirarchy
Record your participation in Piazza clicker question Lesson23 Q1
Arrange these classes in heirarchy from most general (super) to most specific (sub)
tiger, animal, feline, mammal
- tiger, animal, feline, mammal
- tiger, feline, mammal, animal
- animal, mammal, feline, tiger
- animal, tiger, feline, mammal
Activity - Adding to the heirarchy
Record your participation in Piazza clicker question Lesson23 Q2
- Draw a diagram of the heirarchy (animal, mammal, feline, tiger).
- Add lion, canine, reptile, wolf to the heiarchy diagram
- What do all animals have in common? What behaviors or properties?
Activity - Substitution Principle
Record your participation in Piazza clicker question Lesson23 Q3
Look at this vehicle heirarchy from your book

Given this method
public void doSomething(Car theCar)
{
...
}
Objects of which classes could be passed to the doSomething method? Check all correct answers
- Vehicle
- Motorcycle
- Car
- Truck
- Sedan
- SUV
Activity - Subclasses
Record your participation in Piazza clicker question Lesson23 Q4
Given this Definition of Vehicle from Wikipedia:
A vehicle is a mobile machine that transports people or cargo.
Which of these objects could be subclasses of Vehicle? Click all that apply.
- snow mobile

- stretch limo

- ship

- horse

Activity - Writing a subclass
Record your participation in Piazza clicker question Lesson23 Q5
Let's make a subclass, Toy, of the Product class we wrote earlier in the semester. A Toy has an appropriateAge variable and methods to get and set the new variable. The getDescription of the Toy returns the name concatenated with the appropriate age.
- Download the project toy_subclass.zip
- Look at the Product class
- Now create a Toy class that is a subclass of Product
- Declare the instance variable appropriateAge
- Implement the constructor
- The constructor takes the description, price, and appropriate age as parameters.
- Provide Javadoc for the constructor
- Call super class constructor to handle the instance variables defined in Product
super(..., ...)
- Initalize remaining instance variable(s)
- Implement getAppropriateAge
- Provide Javadoc
- We will override getDescription later
Concept - Subclass inherits ...
- The methods of the super class.
- The instance variable of the super class
- But these can only be accessed through the public interface - the public methods.
Concept - Methods in a subclass
Three types
- The subclass can inherit methods from the super class.
- It can create new methods.
- The subclass can override a method in the super class to do what is needed in the subclass
Activity - Override getDescription
Record your participation in Piazza clicker question Lesson23 Q6
- getDescription needs to return the description plus the appropriate age.
- We will override getDescription
to return something like this
Duplo Rocketship Age: 2
Activity - Testing the Toy class
Record your participation in Piazza clicker question Lesson23 Q7