[comp.object] encapsulation

yunyau@bingvaxu.cc.binghamton.edu (Yun Yau Shih) (05/16/91)

Inheritance in this text means direct inheritance.

Suppose B inherits from A. And C, and D inherite from B. 
Now suppose we want to delete B. How can we manage the abstract data of
B. Is there any method which will not violate the encapsulation concept?

A, B, C, and D are classes. 
-- 

yunyau

           "He who laughs --- lasts!"

sakkinen@jyu.fi (Markku Sakkinen) (05/16/91)

In article <1991May16.004216.658@bingvaxu.cc.binghamton.edu> yunyau@bingvaxu.cc.binghamton.edu (Yun Yau Shih) writes:
>Inheritance in this text means direct inheritance.
>
>Suppose B inherits from A. And C, and D inherite from B. 
>Now suppose we want to delete B. How can we manage the abstract data of
                ^^^^^^^
>B. Is there any method which will not violate the encapsulation concept?
>
>A, B, C, and D are classes. 

Solution 1:  Don't succumb to that irrational desire! :-)
   But even earnestly speaking: why?

Solution 2:  Of course we must suppose first that no direct instances of B
exist.  Simply take all data components (instance variables) and all
operations (instance methods) from the definition of B and copy them
into the definitions of both C and D.

Those operations that already have overriding definition in the subclass
(C or D) should _not_ be copied as such.  If the superclass (B) operation
is not invoked anywhere in the subclass, it need not be copied at all.
If it _is_ invoked (e.g. using 'super' in Smalltalk), it must be copied
under a different name and all invocations must be changed accordingly.

Different languages present different additional problems.
For instance, if B has class variables in Smalltalk or 'static' variables
in C++, you must do something clever about them.
Likewise, sophisticated method combinations in CLOS (and other languages
that support them) must be carefully thought about.
Indeed, is the need to eliminate class B worth all the trouble?
Already the fact that it has (at least) two immediate subclasses
tends to suggest that it has reasons for existence.

Markku Sakkinen
Department of Computer Science and Information Systems
University of Jyvaskyla (a's with umlauts)
PL 35
SF-40351 Jyvaskyla (umlauts again)
Finland
          SAKKINEN@FINJYU.bitnet (alternative network address)