From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!mnetor!uunet!husc6!linus!sdl From: sdl@linus.UUCP (Steven D. Litvintchouk) Newsgroups: comp.lang.ada Subject: Re: Inheritance and Ada Message-ID: <19902@linus.UUCP> Date: 17 Dec 87 03:47:00 GMT References: <8712161858.AA06960@techunix.bitnet> Organization: The MITRE Corp., Bedford, MA In-reply-to: ephraim@techunix.BITNET's message of 16 Dec 87 20:32:43 GMT List-Id: Posting-Front-End: GNU Emacs 18.47.1 of Sun Aug 2 1987 on linus (berkeley-unix) > I am an Ada beginner so please excuse me if this question is not too > bright. I am familiar with other object-oriented languages like C++ > and Lisp Flavours and wish to know if Ada supports inheritance as the > other two do. Please reply by e-mail as this newsgroup doesn't arrive > at our site. I will both email you my answer and post it to this newsgroup: Ada does *not* provide direct support for subclassing/inheritance in the style of Smalltalk, Flavors, etc. (And yes, I have found this to be a major limitation of Ada.) You can *simulate* inheritance in Ada, if you're willing to do some extra work. Two techniques are: 1. Use derived types. This works mostly when the subclass differs from the parent class in such simple ways as range constraints. 2. Use generics. The generic formal parameters correspond to the "inherited" scope (e.g. methods). This rather general solution is admittedly clumsy to program. However, we at MITRE have worked out what we believe is a reasonably complete solution to this, for arbitrary semantic dependency networks of data types (including cycles; i.e. mutually recursive data types). The important thing to watch out for is this: Languages like Smalltalk & Flavors distinguish between clients (users) that make instances of a class, and clients that define a subclass of a class. In the former case, the instances are true "black boxes"; i.e. the instance variables are hidden; they can only be modified indirectly via message sends. In the latter case (inheritance), the subclass "sees" everything, including the instance variables. This makes it easy to add methods to the subclass that manipulate the instance variables defined in the parent class. (Although there has been some discussion that this much visibility in Smalltalk is overkill, and you could conceivably make do with less.) This distinction between those users of a class who make instances, and those who make subclasses, does not exist in Ada. Regardless of whether you try to simulate inheritance via derived types, generics, etc., you find that there is a conflict in Ada between encapsulation and inheritance. If you defined a "class" (abstract data type) in Ada, whose types are private (i.e. instance variables are encapsulated or hidden), then if you "inherit" this into a subclass, you have no way to get at the instance variables of the parent class from the subclass. On the other hand, you could make the abstract data type definition of the parent class visible. Then its instance variables can be "inherited," but you have to violate encapsulation to accomplish this. So the instance variables of each instance of the class are not protected from manipulation by others. Steven Litvintchouk MITRE Corporation Burlington Road Bedford, MA 01730 Fone: (617)271-7753 ARPA: sdl@mitre-bedford.arpa UUCP: ...{cbosgd,decvax,genrad,ll-xn,philabs,security,utzoo}!linus!sdl