Inheritance, Exercises

A

Create two classes A and B with default constructors that announces themselves. Inherit a new class called C from A and create a member of B within C. Do not create a constructor for C. Create an object of class C and explain the behavior.

B

Modify Exercise A so that A and B have constructors with arguments. Write a constructor for C and perform all initialization within C's constructor.

C

Create a class inside a package that contains a protected method. Outside of the package try to call the method and explain the results. As a next step inherit from the class and call the protected method again from within the derived class. Explain the difference.

D

Create a class Vehicle and add the appropriate methods to the class. Create a class Car that inherits from Vehicle, add or override the appropriate methods. In the main method create a Car object, upcast it to Vehicle and check which method can be called on the upcasted object.

Extra Assignments

E1

A part can be either simple or composite (consists of parts that consists of parts and so on, i.e., a tree structure). Each part has a price, as an example a simple part has the price listed for the part, whereas the price for a composite part is the sum of all the parts that make up the composite part. Use the composite design pattern to implement a method price on both simple and composite parts.

E1

Build a two version of a simple class with a method calc that does a small computation, e.g., adding two integers and returning the result. For the first version of class the calc method should be final for the other version it should not be final. Measure if the use of final has a positive effect on the execution speed.