Home » SQL & PL/SQL » SQL & PL/SQL » Incapsulation of instance variables in object type (Oracle9i Enterprise Edition Release 9.2.0.4.0 - 64bit Production)
Incapsulation of instance variables in object type [message #291476] Fri, 04 January 2008 05:50 Go to next message
Buchas
Messages: 83
Registered: March 2006
Member
Hello,

I have created an object type like this:
Header:
CREATE OR REPLACE TYPE CommonObj IS OBJECT
(
            nID          NUMBER,
  MAP MEMBER FUNCTION GetID RETURN NUMBER ,
  MEMBER PROCEDURE SetID ( nIDParm IN NUMBER )
  ) ;

Body:
CREATE OR REPLACE TYPE BODY CommonObj AS
   
  -----------Set & get-----------
MAP MEMBER FUNCTION GetID RETURN NUMBER IS
  BEGIN
    RETURN nID;
  END;
  -----
 MEMBER PROCEDURE SetID ( nIDParm IN NUMBER ) IS
  BEGIN
    nID := nIDParm;
  END;
END;

What I do not like, is that anybody can create instances of CommonObj anywhere in database and use it the way they want. Now it is possible to access variable nID directly from anywhere:
aCommonObj.nID
.
I would like to force everybody to access instance variable nID only through the methods GetID and SetID. (So called private variable in C++).

Is there a way to implement that?
Re: Incapsulation of instance variables in object type [message #291479 is a reply to message #291476] Fri, 04 January 2008 06:03 Go to previous messageGo to next message
Michel Cadot
Messages: 64131
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Put the variable in the body and not the definition part.
Definition is PUBLIC part.
Body is PRIVATE part.

Regards
Michel
Re: Incapsulation of instance variables in object type [message #291494 is a reply to message #291479] Fri, 04 January 2008 06:48 Go to previous messageGo to next message
Buchas
Messages: 83
Registered: March 2006
Member
How do I put it there?
If I try doing it this way:
CREATE OR REPLACE TYPE BODY CommonObj AS
   nID NUMBER;
  -----------Set & get-----------
MAP MEMBER FUNCTION GetID RETURN NUMBER IS
  BEGIN
    RETURN nID;
  END;
  -----
 MEMBER PROCEDURE SetID ( nIDParm IN NUMBER ) IS
  BEGIN
    nID := nIDParm;
  END;
END;

it gives compilation errors.
Re: Incapsulation of instance variables in object type [message #291497 is a reply to message #291494] Fri, 04 January 2008 07:18 Go to previous messageGo to next message
Michel Cadot
Messages: 64131
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:

it gives compilation errors.

I don't see any in your post.

Regards
Michel
Re: Incapsulation of instance variables in object type [message #291498 is a reply to message #291497] Fri, 04 January 2008 07:26 Go to previous messageGo to next message
Buchas
Messages: 83
Registered: March 2006
Member
The error is:
"PLS-00103: Encountered the symbol "NID" when expecting one of the following: not final instantiable order overriding static member constructor map"
Re: Incapsulation of instance variables in object type [message #291499 is a reply to message #291494] Fri, 04 January 2008 07:27 Go to previous messageGo to next message
Michel Cadot
Messages: 64131
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
It seems you can't have private variable with types.
Syntax diagram in SQL Reference does not allow it.
Sorry, you can't do what you want this way.

Regards
Michel
Re: Incapsulation of instance variables in object type [message #291523 is a reply to message #291498] Fri, 04 January 2008 12:48 Go to previous message
Frank
Messages: 7880
Registered: March 2000
Senior Member
Don't expect objects to resemble anything like OO-behavior you are used to see in (eg) Java, or you will be very disappointed.
Previous Topic: NoLogging at Table Level
Next Topic: Accumulated Total of Sal according to hierarchy (connect by, analytic funcion)
Goto Forum:
  


Current Time: Wed Dec 07 12:44:51 CST 2016

Total time taken to generate the page: 0.10029 seconds