You are on page 1of 10

PS/2021/078 TUTORIAL 08 2024/02/17

1)
PS/2021/078 TUTORIAL 08 2024/02/17
PS/2021/078 TUTORIAL 08 2024/02/17
PS/2021/078 TUTORIAL 08 2024/02/17

ii)
Object c2 cannot call methods getHeight(), setHeight(), and getVolume() because it's declared as type
Circle, but it's actually an instance of the Cylinder class, which has these methods. Since c2 is treated as
a Circle type, it can only access methods defined in the Circle class.

Object c5 cannot call methods setHeight() and getVolume() for the same reason as c2

Iii)

In each class, there is more than one constructor defined. This is called as constructor overloading.
Constructor overloading allows a class to have multiple constructors with different parameter lists. This
enables objects to be initialized in different ways depending on the arguments provided to the
constructor.

2)
PS/2021/078 TUTORIAL 08 2024/02/17
PS/2021/078 TUTORIAL 08 2024/02/17

IV. No, you cannot create Employee objects directly because it is an abstract class. Abstract classes
cannot be instantiated directly; they are meant to be extended by other classes.

V. If you change the access modifier from public to private in the name, age, and hourRate instance
variables, it will result in a compilation error in the Manager and Clerk classes because they are trying to
access these variables from the superclass Employee. Since these variables are private, they are not
accessible outside the Employee class.
PS/2021/078 TUTORIAL 08 2024/02/17

3)
PS/2021/078 TUTORIAL 08 2024/02/17
PS/2021/078 TUTORIAL 08 2024/02/17
PS/2021/078 TUTORIAL 08 2024/02/17

4)

1. In order to compile the above code, besides the missing constructors, the Car class needs to
implement the tuneUpCost() and canCarry(int numPassengers) methods from the IVehicle interface.
Similarly, the Bicycle class needs to implement the tuneUpCost() and canCarry(int numPassengers)
methods.

2. No, the builtBefore method should not be added to the IVehicle interface because it is specific to the
Car class. Interfaces should define behaviors that are common to all implementing classes, and
builtBefore is not a behavior that applies to all vehicles. It is a specific behavior of a Car object.

3. In the blank line, you can use either the IVehicle interface or the Bicycle class types.

4. On the oldCar object with the type IVehicle, you can only call the methods defined in the IVehicle
interface, which are tuneUpCost() and canCarry(int numPassengers).

5. On the oldCar object with the type Car, you can call all methods defined in the Car class, including
tuneUpCost(), canCarry(int numPassengers), and builtBefore(int otherYear).

You might also like