Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!apple!ames!lll-winken!uunet!wiley!poseidon!simpson From: simpson@poseidon.uucp (Scott Simpson) Newsgroups: comp.sw.components Subject: Re: Inheritance vs. component efficienc Message-ID: <5021@wiley.UUCP> Date: 12 Jun 89 19:01:35 GMT References: <5186@pt.cs.cmu.edu> <5740@hubcap.clemson.edu> Sender: news@wiley.UUCP Reply-To: simpson@poseidon.UUCP (Scott Simpson) Organization: TRW Arcadia Project Lines: 120 In article <5740@hubcap.clemson.edu> billwolf%hazel.cs.clemson.edu@hubcap.clemson.edu writes: >From article <5186@pt.cs.cmu.edu>, by ram@wb1.cs.cmu.edu (Rob MacLachlan): >> it or modify it. The advantage of an o-o inheritance system is that it >> makes it very easy to reuse a component *with modifications*. If you have > > The same thing is possible through the use of Ada packages; a new > package serves as an interface which hides both the calls to the old > package and the new code which was added on top of it, thus preserving > the integrity of the original abstraction and creating a new one. Do The same thing is not possible using layered Ada packages. You will still have to write code to handle the added state. Ada's inheritance model is quite limited compared to C++ and Smalltalk. Here are a couple of problems with Ada inheritance: 1) You only inherit a single type and all its operations. For example, package Demo is type A is limited private; type B is limited private; procedure X(P1 : A; P2 : B); end Demo; with Demo; use Demo; procedure Illustrate is type S is new A; -- You get "procedure X(P1 : S; P2 : B)". type T is new B; -- You get "procedure X(P1 : A; P2 : T)". end Illustrate; How do you get "procedure X(P1 : S; P2 : T)"? You can't. 2) You cannot add state to an Ada type and have inherited operations work. In C++ you can. This makes user interface construction using Ada not quite as clean as an object-oriented approach. User interfaces toolkits often have quite a lot of commonality (what Stroustrup calls the litmus test of OOP) and inheritance seems to close the gap between the abstract and concrete model quite well. To illustrate why Ada's inheritance models fails the OOP test, let's look at an example. Suppose I have an inheritance tree that looks like Button | Text Button | Radio Button Let me try to model it in Ada: -- Package is hypothetical. Operations and data are incomplete. package Button is type Button_State is record Enabled : boolean; -- Can be pressed. Chosen : boolean; -- Currently toggled on. Hit : boolean; -- Currently being pushed. end record; function Create(b : Button_State) return Button_State; procedure Delete(b : Button_State); procedure Draw(b : Button_State); procedure Enable(b : Button_State); procedure Disable(b : Button_State); end Button; with Button; package Text_Button is type Button_State is new Button.Button_State; type Text_Button_State is record Ancestor_State : Button_State; Text : String(1..80); -- String in button. Background : String(1..80); -- Background color. end record; function Create_Text_Button(tb : Text_Button_State) return Text_Button_State; procedure Delete(tb : Text_Button_State); end Text_Button; with Text_Button; package Radio_Button is type Text_Button_State is new Text_Button.Text_Button_State; type Radio_Button_State is record Ancestor_State : Text_Button_State; Off_Value : Integer; -- Value when turned off. end record; procedure Redraw(rb : Radio_Button_State); function Create_Radio_Button(rb : Radio_Button_State) return Radio_Button_State; end Radio_Button; with Radio_Button; use Radio_Button; procedure Main is Radio_Button_Instance : Radio_Button_State; begin -- Number of periods determines how far up in the inheritance tree -- relative to here the field we are looking for is. Radio_Button_Instance.Ancestor_State.Ancestor_State.Enabled := true; -- Delete(Radio_Button_Instance); -- Illegal! Delete(Radio_Button_Instance.Ancestor_State); -- Not what we want. end Main; Notice that once I added state, my operations no longer work. There are a few ways to get around this limitation: 1) Repeat operations at each level. 2) Try a different model. 3) Use a different language. We thought about using Classic Ada (a superset of Ada with Smalltalk type inheritance and dynamic binding) or C++. InterViews is a C++ toolkit done at Stanford which exploits C++s inheritance. It's structure is quite elegant. Scott Simpson TRW Space and Defense Sector oberon!trwarcadia!simpson (UUCP) trwarcadia!simpson@usc.edu (Internet)