Xref: utzoo comp.lang.eiffel:1368 comp.object:2495 Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!uunet!pdn!tscs!tct!chip From: chip@tct.uucp (Chip Salzenberg) Newsgroups: comp.lang.eiffel,comp.object Subject: Re: Inheritance and Information Hiding Message-ID: <27A86309.281A@tct.uucp> Date: 31 Jan 91 18:33:45 GMT References: <10612@pasteur.Berkeley.EDU> <485@eiffel.UUCP> <488@eiffel.UUCP> Organization: Teltronics/TCT, Sarasota, FL Lines: 51 According to bertrand@eiffel.UUCP (Bertrand Meyer): >A class POLYGON exists and has a procedure >``add_vertex'', which it exports. You want >to add a class RECTANGLE. What do you do? > >1. Decide not to use inheritance. >2. Redo the existing inheritance hierarchy. >3. Accept that RECTANGLE inherits from POLYGON and does not export > ``add_vertex''. ... and disallow polymorphic assignments of the form > p := r where p is of type POLYGON and r of type RECTANGLE. >4. Accept that RECTANGLE inherits from POLYGON and does not export > ``add_vertex''. Permit polymorphic assignments of the above form; > but have the type checker reject as invalid any system which would > contain both such an assignment and a call p.add_vertex (...), since > it may lead to a run-time inconsistency. In C++, the add_vertex() member function would almost certainly be virtual; that is, it would act correctly in cases where a |POLYGON*| actually points to an object of type |RECTANGLE|. (If it were not virtual already, it could be made so by the insertion of the word "virtual" at one place in the code.) Given this language feature, add_vertex becomes a non-problem. The implementor of RECTANGLE simply provides an appropriate definition of the RECTANGLE::add_vertex(): a do-nothing function body that returns a failure indication (if appropriate). Analyzing this use of C++ results in a fifth item for Bertrand's list: 5. Accept that RECTANGLE inherits from POLYGON and exports add_vertex, and require the programmer to provide an implementation of add_vertex appropriate for a RECTANGLE. In the real world, how is this solution deficient? >As an aside (meant to be thought- rather than flame-provoking) >I have come to realize, although not early enough, that >information hiding is a greatly overrated goal. Which reminds me of a nice phrase from Leo Brodie's _Thinking_Forth_, that goes something like this: Some programmers use information hiding as if programs are prone to attack from roving bands of hostile programmers. Of course, Forth isn't exactly my favorite programming environment; I prefer driving with seatbelts (but not a padded cell). :-) -- Chip Salzenberg at Teltronics/TCT , "I want to mention that my opinions whether real or not are MY opinions." -- the inevitable William "Billy" Steinmetz