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.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8b95e4156b22b891 X-Google-Attributes: gid103376,public From: franke@minet.uni-jena.de (Frank Ecke) Subject: Re: Constructors? Date: 1998/08/25 Message-ID: #1/1 X-Deja-AN: 384474500 References: <35DF0616.77764632@tech.swh.lv> Organization: Friedrich-Schiller-University Jena, Germany Reply-To: franke@minet.uni-jena.de Newsgroups: comp.lang.ada Date: 1998-08-25T00:00:00+00:00 List-Id: On Sat, 22 Aug 1998 20:55:34 +0300, Maxim Senin wrote: >I'm new to ADA. I'm looking for tips/tricks on how to write constructors. As I >understand, in ADA I have to rewrite constructor completely for each subclass. >How can I reduce code duplication? Can superclass constructor be called from >descendent class constructor? Use the name Ada, please. Yes, you can call a superclass constructor from a descendent class constructor. But beware, although the term constructor exists in Ada, it should not be confused with the notion used in OO. For example, in Java, constructors are not inherited by the subclass. In Ada, a constructor is simply a primitive operation of a tagged type and, therefore, it is propagated into the subclass. Consider, for illustration, a simple hierarchy of geometrical figures (Point, Circle, Rectangle, etc.): type Colored_Point is tagged limited record This_X : Float := 0.0; This_Y : Float := 0.0; This_Color : Color_Type := Black; end record; type Rectangle is new Colored_Point with record This_Length, This_Width : Natural := 1; end record; procedure Init_New_Colored_Point( This_Figure : in out Colored_Point; Init_X, Init_Y : Float := 0.0; Init_Color : Color_Type := Black) is begin Set_XY(This_Figure, Init_X, Init_Y); Set_Color(This_Figure, Init_Color); end Init_New_Colored_Point; procedure Init_New_Rectangle( This_Figure : in out Rectangle; Init_X, Init_Y : Float := 0.0; Init_Color : Color_Type := Black; Init_Length, Init_Width : Natural := 1) is begin Init_New_Colored_Point( Colored_Point(This_Figure), -- effectively ignore the Rectangle -- components Init_X, Init_Y, Init_Color); -- calls the ``constructor'' of the superclass This_Figure.This_Length := Init_Length; This_Figure.This_Width := Init_Width; end Init_New_Rectangle; Note that since Init_New_Colored_Point is inherited by Rectangle, there is the risk of mixing up the various constructor calls. A bit of care is needed. Hope this helps. Regards, Frank