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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fac41,953e1a6689d791f6 X-Google-Attributes: gidfac41,public X-Google-Thread: fdb77,953e1a6689d791f6 X-Google-Attributes: gidfdb77,public X-Google-Thread: f79bb,953e1a6689d791f6 X-Google-Attributes: gidf79bb,public X-Google-Thread: 1108a1,953e1a6689d791f6 X-Google-Attributes: gid1108a1,public X-Google-Thread: 103376,953e1a6689d791f6 X-Google-Attributes: gid103376,public From: jsa@alexandria (Jon S Anthony) Subject: Re: Eiffel and Java Date: 1996/11/01 Message-ID: #1/1 X-Deja-AN: 193848958 sender: news@organon.com (news) references: <55562c$nkd@mulga.cs.mu.OZ.AU> organization: Organon Motives, Inc. newsgroups: comp.lang.eiffel,comp.lang.ada,comp.lang.sather,comp.lang.java.advocacy,comp.object Date: 1996-11-01T00:00:00+00:00 List-Id: In article <1996Oct31.162218.8386@schbbs.mot.com> shang@corp.mot.com (David L. Shang) writes: > In article <55562c$nkd@mulga.cs.mu.OZ.AU> fjh@mundook.cs.mu.OZ.AU (Fergus > Henderson) writes: > > Suppose I have two existing library classes (perhaps supplied by different > > vendors) which have some commonality, but don't inherit from a common > > base class. In Sather, one can simply create a new interface and > > declare these classes to be instances of this interface, without > > modifying the existing code. > > > > (Is that possible in Java?) > > > > No. Java supports only top-down class hierarchy construction > (from superclass to subclasses), but not bottom-up: from > subclasses to superclass. But in general it is _trivial_ to do in Ada. Note that "inheritance" aspects need not (and typically will not) even enter into the "equation" since the functionality is provided by separating the "module" aspect of class from the "type" aspect and allowing the module aspect to have separate spec. and impl. > Suppose we have two existing classes > > class Stack #(MemberType: type of any) > { > function push(MemberType); > function pop:MemberType; > } > class Queue #(MemberType: type of any) > { > function push(MemberType); > function pop:MemberType; > } > > and later, we decide to have a superclass from them, we can have > > class StackOrQueue #(MemberType: type of any) > is super Stack, Queue > { > function push(MemberType); > function pop:MemberType; > } > > without modifying the existing subclasses. > > Now, we can write polymophic code like: > > x: StackOrQueue; > if (some_condition) x = Stack#(T1)(); else x:=Queue#(T2)(); > y: any = GetObject(); > assume (y is x#.MemberType) x.push(y); otherwise do_nothing(); OK, this example is a little more involved... with Stacks; -- Generic with Queues; -- Generic generic type Any_Obj is tagged private; package Stack_Or_Queue package S is new Stacks(Any_Obj); package Q is new Queues(Any_Obj); type Any is access all Any_Obj'Class; type Stack_Queue ( S : access S.Stack; Q : access Q.Queue ) is private; ... procedure Push (Element : Any; Onto : Stack_Queue); function Pop (SQ : Stack_Queue) return Any; ... private type Stack_Queue ... ... end Stack_Or_Queue; with Stack_Or_Queue; ... package SQ_T1 is new Stack_Or_Queue(T1); package ST1 renames SQ_T1.S; package SQ_T2 is new Stack_Or_Queue(T2); package QT2 renames SQ_T2.Q; ... S : ST1; Q : QT2; ... if (some condition) then declare X : SQ_T1(S, null); -- X is a stack of T1s Y : SQ_T1.Any := Get_Object; begin push(Y, Onto => X); end; else declare X : SQ_T2(null, Q); -- X is a queue of T2s Y : SQ_T2.Any := Get_Object; begin ... end if; ... /Jon -- Jon Anthony Organon Motives, Inc. Belmont, MA 02178 617.484.3383 jsa@organon.com