Object Oriented Design Course

Object Oriented Design - Exercise 1

Self Learn about Inheritance, Typing, the LSP and Multiple Dispatch

This is the material covered in this exercise:

bullet

Using Inheritance Well: Common mistakes and a taxonomy of proper uses.

bullet

Static Typing, and the problems caused by Covariance and Descendant Hiding.

bullet

The Liskov Substitutability Principle, an important OOD principle.

bullet

Multiple Dispatch, a useful feature not supported by mainstream object-oriented languages, and a C++ implementation of them.

The deadline for this exercise is Thursday, March 6th, 2003.

Using Inheritance Well

Read this paper first; only sections 25.1 to 25.5 are required. The book (OOSC2) from which this text is taken is available in the library as well. The sample code is in Eiffel, so "feature" means "method", "unique" means "const", "deferred" means "abstract" and "effective" means "concrete (not abstract)". After reading the first five sections, Answer the following questions:

1. Give examples for violating the "rule of inheritance", "rule of change" and the "taxomania rule". Give one example for each rule, in either C++ or Java. Your examples should be noticeably different from the ones given in the paper.

2. Eiffel has many valid uses for inheritance, as section 25.5 shows. Divide the ten uses to those which are implemented in C++ using public inheritance, those which are implemented using private inheritance, and those which cannot or should not be implemented using inheritance at all. For the third group, show the language feature that replaces inheritance.

Static Typing, Covariance and Descendant Hiding

Read this paper first, and answer the following questions:

3. Regarding the difference between typing and binding:

bullet

C++ offers static binding by default, while Java supports dynamic binding by default. Show how to declare a method in each language that override that language's default.

bullet

Show an example of how static typing of classes (not primitive types) can by bypassed in C++, so that a type error can occur at runtime. Why can't the same thing happen in Java?

4. Give one example (possibly from the skiers' classes) for each of the following:

bullet

a polymorphic catcall

bullet

a non-polymorphic catcall

bullet

a polymorphic non-catcall

bullet

a non-polymormic non-catcall

The Liskov Substitutability Principle

Read this paper first, and answer the following questions:

5. How should the classes Square and Rectangle from the paper be modeled?

6. For each of the following, state whether it does or doesn't obey the LSP, and briefly explain your answer:

bullet

Covariant argument types (the type of a method's argument is redefined with covariance)

bullet

Covariant return types (the type of a method's return value is redefined with covariance)

bullet

Contra-variant argument types

bullet

Contra-variant return types

bullet

Descendant Hiding

Multiple Dispatch

Read this paper first, and answer the following questions:

7. Assume that multiple dispatch is not available, and you must code intersect() for four Shape descendant classes - Circle, Square, Rectangle, Ellipse. Each shape can be intersected with any other, in any order: if, for example, c is a circle and s is a square, then both c.intersect(s) and s.intersect(c) must be supported. Compare the amount of code and the number of compilation dependencies between implementing these methods with and without using multiple dispatch.

8. For each of the two implementations of multiple dispatch the paper offers, explain why it wouldn't work if C++ had no support for virtual inheritance, and normal inheritance would have been used instead).

Submission

Our course uses the department's regular submission system, which means that your first step is to register for the course. Once that's done, submit the exercise electronically only. The file format can be plain text, PDF, Postscript or Word. Word files should be compatible with Word 2000, and can be in either Hebrew or English.

All eight questions must be answered. Length is not officially limited, but do keep your answers to the point.

Good luck!