CS49J Lesson 16 – Polymorphism

Ch Ch 9: 9.4

Big Java 6

When you come in

  1. Connect to the Internet
  2. Log in to Piazza
  3. Start Eclipse or your IDE
  4. Navigate to laughton.com/obrien/sjsu/
    cs49j/lessons and open this lesson

Questions

Activity - super class and subclass

Record your participation in Piazza clicker question Lesson16 Q1

Think about the Product and Toy classes from last lesson. Toy is a subclass of Product. Assume product  is an object of the class Procuct and littlePeople is an object of the class 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 Lesson16 Q2

 

Activity - Object reference variable

Record your participation in Piazza clicker question Lesson16 Q3

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 Lesson16 Q4

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

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

 

 

Activity - Casting

Record your participation in Piazza clicker question Lesson16 Q5

You have a Product object reference, thing. thing actually references a Toy object (rocket). You want to know the appropriate age. What code can you use to do that?