CEL - Extending the Celebrity Class (Lesson)

APCompSci_LessonTopBanner.png

Extending the Celebrity Class

Introduction

 

In this lab activity you will have the chance to design your own subclass of Celebrity. Spend some time thinking about the different attributes and behaviors that you might want to include in this new class. Time spent on the design before beginning implementation can cut down on the time needed to implement a class.

Extending the Celebrity Class

At this point the Celebrity game is functional with a generic Celebrity class. While this is a perfectly valid implementation, it’s also somewhat limited. Every celebrity that is created has the same attributes. But what if you wanted to create a more specific type of Celebrity? One that had additional attributes and behaviors apart from what all Celebrity objects have? By using inheritance to extend the Celebrity class, you can do just that.

Think about a subset or certain classification of celebrity that would have additional attributes or behaviors. If you were to incorporate a different type of celebrity into the Celebrity game, what kind of celebrity would you want to make?

Record the answer to the following question on a sheet of paper. You will need the answer to complete the Lab Check Quiz.

  • Identify the attributes and behaviors that belong to this new type of celebrity that are separate from a "default" celebrity.

Class Name Chart 

It’s important to spend time on the design before implementation begins. This step cannot be overstated, as the use of inheritance in a program signifies a relationship between objects and has the goal of code reuse and sharing information.

Look at the supplied LiteratureCelebrity class. It has an ArrayListString for the clues that are associated with it. It also has a constructor with two parameters just like the Celebrity class. There is a method processClues that is called in the constructor and the overridden getClue method that maintains the integrity of the original data (clue). There is another overridden method toString that uses the super.getClue method to access the original data from the Celebrity class clue variable as well. This is one of the benefits of using inheritance in a design, since superclass methods can easily be called and used in the subclass. All subclasses need to have a clear relationship to the superclass and should provide additional, separate functionality from the superclass to differentiate it from the superclass.

The @Override prefix is provided as a cue to other developers to formally express that you as a developer are changing what is done in this subclass. It’s not a requirement in Java but is an accepted practice to provide good documentation.

In this lab activity, you will create the Celebrity subclass you designed earlier.

2. Create the Java file for your Celebrity subclass.

3. Define the instance variables for the Celebrity subclass.

4. Write the constructor for your class.

5. Override the getClue and/or the getAnswer method(s).

6. Override the toString method.

7. Write any other methods that your design has indicated will be required. Look over your design plan and make sure that you have implemented all required components. Check that your code compiles, making sure to test any methods you write.

Lab content from the College Board.

Check Your Understanding

Record the answers to the following questions on a sheet of paper. You will need the answers to complete the Lab Check Quiz.

17. How do we identify if a method is an overridden method?

18. How do we send information from the subclass to the superclass?

19. What keyword is used in Java to identify inheritance?

20. What method is executed when an ArrayList is made of the superclass but a subclass instance is stored in it?

For example:

ArrayList<Animal> zooList = new ArrayList<Animal>( ) ;
zooList.add(new Gryphon ( ) ) ;
Animal temp = zoolist.get ( (int) Math.random ( ) * zooList.size ( ) ) ;
temp.playSound ( ) ; 

APCompSci_LessonBottomBanner.pngIMAGES CREATED BY GAVS