Workshop
本页目录· 4 条
11.1 Activity: Designing inheritance hierarchies
Task 1
Order the following lists of items into an inheritance hierarchy.
(a) banana, cake, bread, fruit, food item, pasta, apple, dessert, jelly, bagel
(b) vehicle, aircraft, boat, helicopter, bicycle, hovercraft, plane, car, bus, watercraft, spacecraft, motor vehicle
Task 2
Draw an inheritance hierarchy for people at Monash University.
Some types of people you might include are:
student, undergraduate student, postgraduate student, staff, lecturer, teaching assistant, Monash community member, gardener, librarian.
How do each of these person types relate to one another?
Answer
Task 1
(a)
food item
bread
cake
bagel
fruit
banana
apple
dessert
cake
jelly
(b)
vehicle
aircraft
plane
helicopter
bicycle
hovercraft
watercraft
boat
spacecraft
motor vehicle
car
bus
Task 2
Monash community member
student
undergraduate student
postgraduate student
staff
lecturer
teaching assistant
gardener
librarian
11.2 Activity: Implementing Inheritance
Write a super class and subclasses in Java to implement the inheritance hierarchy you designed in Activity 11.1 Task 2. (Recall that Java does not support multiple inheritance, so if your inheritance hierarchy illustrates multiple inheritance relationships, you will need to decide how to simplify it.)
Include appropriate constructors, fields, and accessor/mutator methods for each class. Where classes have attributes/methods in common, make sure these attributes/methods are defined as far up the hierarchy as possible.
To get you started, some of the fields you might need are: name, studentId, staffId.