Lesson 23 – Inheritance

Big Java 6

When you come in

  1. Connect to the Internet
  2. Log in to Piazza
  3. Start Bluej
  4. Navigate to laughton.com/obrien/sjsu/cs46a/CRT_lessons/ and open this lesson

Questions

Subclasses and super classes

 

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

  1. tiger, animal, feline, mammal
  2. tiger, feline, mammal, animal
  3. animal, mammal, feline, tiger
  4. animal, tiger, feline, mammal

Activity - Adding to the heirarchy

Record your participation in Piazza clicker question Lesson23 Q2

Substitution Principle

Activity - Substitution Principle

Record your participation in Piazza clicker question Lesson23 Q3

Look at this vehicle heirarchy from your book

subclasses of Vehicle

Given this method

public void doSomething(Car theCar)
{
    ...
}

Objects of which classes could be passed to the doSomething method? Check all correct answers

  1. Vehicle
  2. Motorcycle
  3. Car
  4. Truck
  5. Sedan
  6. 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.

  1. snow mobile snowmobile
  2. stretch limo stretch limo
  3. ship heilcopter carrier
  4. horse 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.

 

Concept - Subclass inherits ...

 

Concept - Methods in a subclass

Three types

 

Activity - Override getDescription

Record your participation in Piazza clicker question Lesson23 Q6

Activity - Testing the Toy class

Record your participation in Piazza clicker question Lesson23 Q7