Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!shelby!neon!craig From: craig@Neon.Stanford.EDU (Craig D. Chambers) Newsgroups: comp.lang.eiffel Subject: Re: Inheritance and Information Hiding Message-ID: <1991Feb6.214110.28256@Neon.Stanford.EDU> Date: 6 Feb 91 21:41:10 GMT References: <1991Feb5.130359.9735@bellcore.bellcore.com> <1991Feb6.045542.791@visix.com> Organization: Stanford University Lines: 46 In article blerner@empire.cs.umass.edu (Barbara Lerner) writes: > > [stuff deleted] > >If we take the point of view that we can add a vertex to a rectangle >with the result begin a pentagon, we now have two choices: > > [stuff deleted] > > 2. Add the vertex to the rectangle object, and change its class > from rectangle to pentagon. > > [stuff deleted] > >The second solution is not type safe in the following situation. >Suppose we have an object O with two fields: p of type polygon, and r >of type rectangle. Now suppose we execute the following code with >add_vertex changing the class of the rectangle to pentagon. > > O.r := make_rectangle(..); > O.p := O.r; > O.p.add_vertex(..); > >If we change the class of the rectangle to pentagon, then O.r now >refers to a pentagon while the class of O.r is declared to be >rectangle. If you take the view that rectangle is a specialized *implementation* of polygons, but the *type* of rectangle is still polygon, then this class-changing operation is perfectly type-safe (as I mentioned in an earlier posting). The add_vertex operation on rectangles modifies the rectangle *in place* to be a pentagon-represented polygon; the interface to the rectangle remains unchanged. This facility requires that some classes be marked as not defining new types but instead new implementations of existing types. I think that enabling the programmer to change the implementation of objects at run-time is a very powerful feature, and programmers will find new and useful ways of applying this power as they become used to thinking about an object's representation and implementation as mutable. This operation is rather complicated to implement efficiently and describe coherently in a class-based language, but is quite straightforward in a prototype-based language supporting dynamic object inheritance (delegation) instead of static class inheritance. -- Craig Chambers