5 Replies Latest reply: May 17, 2011 5:10 PM by gaverill RSS

    supertype constructor problem

    471800
      Hi All,

      I have created 2 objects A and B.
      A is the child of B.

      How can i call the constructor of the B inside A? ie. calling supertype constructor.


      Thanks,
      kt
        • 1. Re: supertype constructor problem
          Zlatko Sirotic
          First, how to call the parent object's version of an overridden method.

          In Oracle 9i / 10g we can do the following
          (originally posted June 2003 - www.quest-pipelines.com/pipelines/plsql/tips03.htm#JUNE):
          Re: how to invoke overriden methods in a super type

          Oracle 11g has a new feature - support for generalized invocation:
          http://download-uk.oracle.com/docs/cd/B28359_01/appdev.111/b28371/whatsnew.htm
          "Generalized invocation syntax is now supported.
          Therefore, a member method in a subtype can statically invoke (dispatch) a member method in any supertype in the supertype hierarchy of the current subtype,
          including the subtype's immediate supertype."


          But, constructor methods cannot be invoked using this syntax!
          User-defined constructors are not inherited, so they cannot be overridden.


          Regards,
          Zlatko
          • 2. Re: supertype constructor problem
            471800
            Hi Zlatko,

            Thank you very much for your time.

            Now am able to call supertype's method using generalization technique.

            eg.(self as AbstractComponent).DisplayMsg.


            Is they any way to call AbstractComponent's constructor if AbstractComponent is the parent? ( like super in java)

            Actually am working on Decorator Design pattern.

            Thanks
            kt
            • 3. Re: supertype constructor problem
              Zlatko Sirotic
              Try this.

              Java code is from
              http://en.wikipedia.org/wiki/Decorator_pattern
              // the Window interface = AbstractComponent
              interface Window {
                  public void draw(); // draws the Window
                  public String getDescription(); // returns a description of the Window
              }
              
              // implementation of a simple Window without any scrollbars = ConcreteComponent
              class SimpleWindow implements Window {
                  public void draw() {
                      // draw window
                  }
               
                  public String getDescription() {
                      return "simple window";
                  }
              }
              
              // abstract decorator class - note that it implements Window
              abstract class WindowDecorator implements Window {
                  protected Window decoratedWindow; // the Window being decorated
               
                  public WindowDecorator (Window decoratedWindow) {
                      this.decoratedWindow = decoratedWindow;
                  }
              }
               
              // the first concrete decorator which adds vertical scrollbar functionality
              class VerticalScrollBarDecorator extends WindowDecorator {
                  public VerticalScrollBarDecorator (Window decoratedWindow) {
                      super(decoratedWindow);
                  }
               
                  public void draw() {
                      drawVerticalScrollBar();
                      decoratedWindow.draw();
                  }
               
                  private void drawVerticalScrollBar() {
                      // draw the vertical scrollbar
                  }
               
                  public String getDescription() {
                      return decoratedWindow.getDescription() + ", including vertical scrollbars";
                  }
              }
               
              // the second concrete decorator which adds horizontal scrollbar functionality
              class HorizontalScrollBarDecorator extends WindowDecorator {
                  public HorizontalScrollBarDecorator (Window decoratedWindow) {
                      super(decoratedWindow);
                  }
               
                  public void draw() {
                      drawHorizontalScrollBar();
                      decoratedWindow.draw();
                  }
               
                  private void drawHorizontalScrollBar() {
                      // draw the horizontal scrollbar
                  }
               
                  public String getDescription() {
                      return decoratedWindow.getDescription() + ", including horizontal scrollbars";
                  }
              }
              
              // Here's a test program that creates a Window instance which is fully decorated
              // (i.e., with vertical and horizontal scrollbars), and prints its description
              public class DecoratedWindowTest {
                  public static void main(String[] args) {
                      // create a decorated Window with horizontal and vertical scrollbars
                      Window decoratedWindow = new HorizontalScrollBarDecorator (
                              new VerticalScrollBarDecorator(new SimpleWindow()));
               
                      // print the Window's description
                      System.out.println(decoratedWindow.getDescription());
                  }
              }
              PL/SQL code - uses procedure WindowDecoratorCode:

              -- the Window interface = AbstractComponent
              CREATE OR REPLACE TYPE Window AS OBJECT (
                 description VARCHAR2 (30), -- in PL/SQL, object type must have atributte(s)
                 NOT INSTANTIABLE MEMBER PROCEDURE draw, -- draws the Window
                 NOT INSTANTIABLE MEMBER FUNCTION getDescription RETURN VARCHAR2 -- returns a description of the Window
              )  NOT FINAL
                 NOT INSTANTIABLE
              /
              
              -- implementation of a simple Window without any scrollbars = ConcreteComponent
              CREATE OR REPLACE TYPE SimpleWindow UNDER Window (
                 OVERRIDING MEMBER PROCEDURE draw,
                 OVERRIDING MEMBER FUNCTION getDescription RETURN VARCHAR2
              )  NOT FINAL
              /
              CREATE OR REPLACE TYPE BODY SimpleWindow IS
                 OVERRIDING MEMBER PROCEDURE draw IS
                 BEGIN
                    NULL; -- draw window
                 END;
              
                 OVERRIDING MEMBER FUNCTION getDescription RETURN VARCHAR2 IS
                 BEGIN
                    RETURN 'simple window';
                 END;
              END;
              /
              
              -- abstract decorator class - note that it implements Window
              CREATE OR REPLACE TYPE WindowDecorator UNDER Window (
                 decoratedWindow Window, -- the Window being decorated
                 CONSTRUCTOR FUNCTION WindowDecorator (decoratedWindow Window) RETURN SELF AS RESULT,
                 MEMBER PROCEDURE WindowDecoratorCode (decoratedWindow Window)
              )  NOT FINAL
                 NOT INSTANTIABLE
              /
              CREATE OR REPLACE TYPE BODY WindowDecorator IS
                 CONSTRUCTOR FUNCTION WindowDecorator (decoratedWindow Window) RETURN SELF AS RESULT IS
                 BEGIN
                    WindowDecoratorCode (decoratedWindow); -- !!!
                    RETURN;
                 END;
              
                 MEMBER PROCEDURE WindowDecoratorCode (decoratedWindow Window) IS
                 BEGIN
                    SELF.decoratedWindow := decoratedWindow;
                 END;
              END;
              /
               
              -- the first concrete decorator which adds vertical scrollbar functionality
              CREATE OR REPLACE TYPE VerticalScrollBarDecorator UNDER WindowDecorator (
                 CONSTRUCTOR FUNCTION VerticalScrollBarDecorator (decoratedWindow Window) RETURN SELF AS RESULT,
                 OVERRIDING MEMBER PROCEDURE draw,
                 OVERRIDING MEMBER FUNCTION getDescription RETURN VARCHAR2,
                 MEMBER PROCEDURE drawVerticalScrollBar
              )  NOT FINAL
              /
              CREATE OR REPLACE TYPE BODY VerticalScrollBarDecorator IS
                 CONSTRUCTOR FUNCTION VerticalScrollBarDecorator (decoratedWindow Window) RETURN SELF AS RESULT IS
                 BEGIN
                    SELF.WindowDecoratorCode (decoratedWindow); -- !!!
                    RETURN;
                 END;
              
                 OVERRIDING MEMBER PROCEDURE draw IS
                 BEGIN
                    drawVerticalScrollBar;
                    decoratedWindow.draw;
                 END;
              
                 OVERRIDING MEMBER FUNCTION getDescription RETURN VARCHAR2 IS
                 BEGIN
                    RETURN decoratedWindow.getDescription || ', including vertical scrollbars';
                 END;
              
                 MEMBER PROCEDURE drawVerticalScrollBar IS
                 BEGIN
                    NULL; -- draw the vertical scrollbar
                 END;
              END;
              /
              
              -- the second concrete decorator which adds horizontal scrollbar functionality
              CREATE OR REPLACE TYPE HorizontalScrollBarDecorator UNDER WindowDecorator (
                 CONSTRUCTOR FUNCTION HorizontalScrollBarDecorator (decoratedWindow Window) RETURN SELF AS RESULT,
                 OVERRIDING MEMBER PROCEDURE draw,
                 OVERRIDING MEMBER FUNCTION getDescription RETURN VARCHAR2,
                 MEMBER PROCEDURE drawHorizontalScrollBar
              )  NOT FINAL
              /
              CREATE OR REPLACE TYPE BODY HorizontalScrollBarDecorator IS
                 CONSTRUCTOR FUNCTION HorizontalScrollBarDecorator (decoratedWindow Window) RETURN SELF AS RESULT IS
                 BEGIN
                    SELF.WindowDecoratorCode (decoratedWindow); -- !!!
                    RETURN;
                 END;
              
                 OVERRIDING MEMBER PROCEDURE draw IS
                 BEGIN
                    drawHorizontalScrollBar;
                    decoratedWindow.draw;
                 END;
              
                 OVERRIDING MEMBER FUNCTION getDescription RETURN VARCHAR2 IS
                 BEGIN
                    RETURN decoratedWindow.getDescription || ', including horizontal scrollbars';
                 END;
              
                 MEMBER PROCEDURE drawHorizontalScrollBar IS
                 BEGIN
                    NULL; -- draw the horizontal scrollbar
                 END;
              END;
              /
              
              -- Here's a test program that creates a Window instance which is fully decorated
              -- (i.e., with vertical and horizontal scrollbars), and prints its description
              DECLARE
                 decoratedWindow Window;
              BEGIN
                 -- create a decorated Window with horizontal and vertical scrollbars
                 decoratedWindow := NEW HorizontalScrollBarDecorator
                                       (NEW VerticalScrollBarDecorator
                                            (NEW SimpleWindow ('dummy_desc')));
              
                 -- print the Window's description
                 DBMS_OUTPUT.PUT_LINE (decoratedWindow.getDescription());
              END;
              /
              Regards
              • 4. Re: supertype constructor problem
                471800
                Hi Zlatko Sirotic,


                Fantastic...Thanks a ton

                --Kt                                                                                                                                                                                                       
                • 5. Re: supertype constructor problem
                  gaverill
                  I suspect you don't really need to call the constructor, rather you want to get at some complicated initialization code. So I suggest you refactor your types to include an initialization procedure that you can call from the constructors:
                  create or replace type A as object (
                  
                    ...
                  
                    constructor function A(...) return self as result,
                  
                    member procedure initialize(...)
                  )
                  not final;
                  
                  create or replace type B under A (
                  
                     ...
                  
                    constructor function B(...) return self as result,
                  
                    overriding member procedure initialize (...)
                  );
                  
                  The type bodies would then be:
                  
                  create or replace type body A is
                  
                    constructor function A(...) return self as result
                    is
                    begin
                      self.initialize(...);
                      return;
                    end;
                  
                    member procedure initialize(...)
                    is
                    begin
                      ...      // complicated initialization code moved here
                    end;
                  end;
                  
                  create or replace type body B is
                  
                    constructor function B(...) return self as result
                    is
                    begin
                      self.initialize();
                      return;
                    end;
                  
                    overriding member procedure initialize (...)
                    is
                    begin
                      (self as A).initialize(...);    // call super-type's initialization code
                  
                      ...   // other type-specific initialization code goes here
                    end;
                  end;
                  Hope that helps...

                  Gerard