25, డిసెంబర్ 2013, బుధవారం

Overriding vs hiding in plsql




Code-1 base class definition
CREATE OR REPLACE TYPE rectangle AS OBJECT(length number,width number,
member function enlarge( inc number) return rectangle, NOT FINAL
member procedure display) NOT FINAL;

code-2  body implementation for the base class
//design the body

Code-3  sub class definition
CREATE OR REPLACE TYPE tabletop UNDER rectangle
(material varchar2(20),
OVERRIDING member procedure display) final;      even though u will not tell also by default it is final
/

CREATE OR REPLACE TYPE tabletop UNDER rectangle
(
material varchar2(20);
member procedure ramu )  u can use it if u are decided not implement method override
/

Code -4 inherited class body
CREATE OR REPLACE TYPE BODY tabletop AS
OVERRIDING MEMBER PROCEDURE display IS
BEGIN
dbms_output.put_line('Length: '|| length);
dbms_output.put_line('Width: '|| width);
dbms_output.put_line('Material: '|| material);
END display;
end;
/
Type body created.
SQL> set serveroutput on;
Code-5
SQL> DECLARE
  2  t1 tabletop;
  3  t2 tabletop;
  4  BEGIN
  5  t1:= tabletop(20, 10, 'Wood');
  6  t2 := tabletop(50, 30, 'Steel');
  7  t1.display;
  8  t2.display;
  9  END;
 10  /
Length: 20   Width: 10   Material: Wood
Length: 50    Width: 30  Material: Steel
PL/SQL procedure successfully completed.



Hi Experts,
I am going through the oracle documentation to understand the concept of overriding and hiding. This is the definition from the oracle documentation

"Redefining an inherited method to customize its behavior in a subtype is called overriding, in the case of member methods, or hiding, in the case of static methods."
Link: http://docs.oracle.com/cd/B28359_01/appdev.111/b28371/adobjbas.htm

Could you please provide an example of hiding and how it differs from overridding?
"Redefining an inherited method to customize its behavior in a subtype is called overriding, in the case of member methods, or hiding, in the case of static methods."
Consider this base class:
create or replace type TParentClass as object(
  name varchar2(10),
  static function ClassName return varchar2,
  member function DoSomething return varchar2
) not final;
It defines a static class function and a member class function. Now both can be overridden in asubclass. E.g.

create or replace type TChildClass under TParentClass (
  static function ClassName return varchar2,   // hiding class static method
  overriding member function DoSomething return varchar2  // overriding class member method
) final;

Correction made - the clause overriding is not needed for static class methods, when the subclass defines a static method with the same name as the parent class static method (thus hiding the parent static method via the subclass)
. If both the class can be overriden, how the hiding differs from overriding. It would be great if you give me a complete example.When the overriding or hiding would be useful?
If both the class can be overriden, how the hiding differs from overriding. The static class method is not overridden (I fixed the example I posted above - was not close to an Oracle database over the weekend to confirm the syntax).
The static class method is redefined in the subclass - which means that one cannot call the parent static class method via the child class.It would be great if you give me a complete example.



A basic example below.
SQL> create or replace type TParentClass as object(
  2          name varchar2(10),
  3          static function ClassName return varchar2,
  4          member function DoSomething return varchar2
  5  ) not final;
  6  /

Type created.

SQL>
SQL> create or replace type body TParentClass as
  2          static function ClassName return varchar2 is
  3          begin
  4                  return( 'TParentClass' );
  5          end;
  6 
  7          member function DoSomething return varchar2 is
  8          begin
  9                  return( 'Hello world from '||TParentClass.ClassName );
 10          end;
 11  end;
 12  /

Type body created.

SQL>
SQL> create or replace type TChildClass under TParentClass (
  2          static function ClassName return varchar2,
  3          overriding member function DoSomething return varchar2
  4  ) final;
  5  /

Type created.

SQL>
SQL> create or replace type body TChildClass as
  2          static function ClassName return varchar2 is
  3          begin
  4                  return( 'TChildClass' );
  5          end;
  6 
  7          overriding member function DoSomething return varchar2 is
  8          begin
  9                  return( 'Hello world from '||TChildClass.ClassName );
 10          end;
 11  end;
 12  /

Type body created.

SQL>
SQL> declare
  2          obj     TParentClass;
  3  begin
  4          --// create a parent class object
  5          obj := new TParentClass(null);
  6          DBMS_OUTPUT.put_line( 'obj method DoSomething()  = '||obj.DoSomething() );
  7  end;
  8  /
obj method DoSomething()  = Hello world from TParentClass

PL/SQL procedure successfully completed.

SQL>
SQL> declare
  2          obj     TParentClass;
  3  begin
  4          --// create a child object
  5          obj := new TChildClass(null);
  6          DBMS_OUTPUT.put_line( 'obj method DoSomething() = '||obj.DoSomething() );
  7          --// use "general invocation" to call the overridden parent method
  8          DBMS_OUTPUT.put_line( 'parent method DoSomething() = '||(obj as TParentClass).DoSomething() );
  9  end;
 10  /
obj method DoSomething() = Hello world from TChildClass
parent method DoSomething() = Hello world from TParentClass

PL/SQL procedure successfully completed.

SQL>
SQL> begin
  2          --// not possible to call a hidden parent static class method via
  3          --// the child class
  4          DBMS_OUTPUT.put_line( 'class = '||(TChildClass as TParentClass).ClassName() );
  5  end;
  6  /
        DBMS_OUTPUT.put_line( 'class = '||(TChildClass as TParentClass).ClassName() );
                                           *
ERROR at line 4:
ORA-06550: line 4, column 37:
PLS-00306: wrong number or types of arguments in call to 'TCHILDCLASS'
ORA-06550: line 4, column 2:
PL/SQL: Statement ignored
When the overriding or hiding would be useful?
Hiding the static method as shown above allows the child class to hide the parent's static class method and implement its own static method. The simplistic example above would not work correctly if the child static method showed the name of the parent class. So the child class has to hide the parent static class method and provide its own class method instead.

Normal class methods can be overridden - normal o-o behaviour. The child class provides a different or extended features using the same member method as the parent class. However, the parent method is not hidden. It can still be accessed - and in PL/SQL, it is accessed via a feature called "+generalised invocation/expressions+" as detailed in Oracle® Database Object-Relational Developer's Guide.

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి