Chapter Seven: Type Hierarchy

  • Liskov asserts that a compiler may not be able to determine what code to run when a method is called. Give an example to demonstrate that this is actually true. Furthermore, discuss a solution that will enable the dispatch to the desired method.

The compiler only knows about the apparent type of an object in code. For example, you can declare an object in the following way: OutputStream foo = new FileOutputStream("tester");. The actual type of the object is FileOutputStream, but the apparent object type is OutputStream. Since FileOutputStream contains a method close that overrides the close method of OutputStream, the compiler cannot determine which code should actually be run because it only sees the OutputStream type. Yet, it must call the implementation of close for FileOutputStream.

In order to find the right method, dispatching is done. The compiler generates code to find the proper method's code and then branch to it. One approach is to have all objects contain a dispatch vector. The dispatch vector contains pointers to the implementations of the methods that the object should use. Thus, when code is considered, the object's proper method is found from the dispatch vector, and then it branches to that address. For example, FileOutputStream would contain a pointer to the close method for FileOutputStream. Although foo looks like an OutputStream object, the pointer to the proper method is still there. Thus, the correct method is called.



  • The Java programming language provides the notion of an abstract class and an interface. Provide some guidelines that might enable us to chose between these two constructs.

An abstract class is a partial implementation of a type. Subclasses can implement methods from the abstract class, or it can define methods of its own (or it could override methods from the abstract class). An interface provides no implementation for the methods, only their signatures. All methods are nonstatic and public. Another difference is that you can extend only one abstract class but that you can implement many interfaces.



  • Clearly describe the Liskov substitution principle. Make sure that your response includes a review of the three rules that must be supported. What is the main purpose of this principle? What insight does this principle on an inheritance hierarchy?

The Liskov substitution principle ensures that an inheritence hierarchy is correct. In the Liskov substitution principle (simply "the substitution principle" in the text), an object created by a subclass must behave the same as an object created by a supertype. There are three rules provided for the substitution principle. The first is called the signature rule. It states that subtypes have all the same methods as the supertype. Another condition of this rule is that the signature of subtype methods must be compatible with those of the supertype. The second rule is called the methods rule. This rule states that calls made by subtypes methods should behave similar to calls made by their supertype. The third rule is called the properties rule. It states that subtype must preserve all properties that can be proved about supertype objects. The first rule is enforced by the compiler, but the other two programmers need to worry about.



Kristen Walcott and Brian Blose

Links to this Page