Lesson 24 – Inheritance and Polymorphism

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

Activity - super class and subclass

Record your participation in Piazza clicker question Lesson24 Q1

Think about the Product and Toy classes from last lesson. Toy is a subclass of Product. product and littlePeople are defined as:

  Product product = new Product(....);
  Toy littlePeople = Toy(....);
  
Which of the following calls are legal? (May be multiple answers)

  1. product.getPrice()
  2. littlePeople.getPrice()
  3. product.getAppropriateAge()
  4. littlePeople.getAppropriateAge()

Polymorphism

Activity - Polymorphism in action

Record your participation in Piazza clicker question Lesson24 Q2

 

Activity - Object reference variable

Record your participation in Piazza clicker question Lesson24 Q3

If we add this code to the ToyTester,

   Product thing = rocket;
   System.out.println(thing.getAppropriateAge());

will it compile?

  1. Yes
  2. No
  3. I have no idea

Activity - Object reference variable

Record your participation in Piazza clicker question Lesson24 Q4

Which of the following method calls are valid, given the declarations in ToyTester. Check all that are valid

  1. rocket.increasePrice(10)
  2. thing.increasePrice(10)
  3. rocket.getDescription()
  4. thing.getDescription()
  5. rocket.getAppropriateAge()
  6. thing.getAppropriateAge()

 

 

Activity - Casting

Record your participation in Piazza clicker question Lesson24 Q5

You have a Product object reference, thing. thing actually references a Toy object (rocket). You want to know the appropriate age. How can you get it?