Forum > General

Please confirm this can't be done...

(1/1)

EganSolo:
I know we can declare protected (or private) properties as public in descendant classes, and I thought we could do the same with methods but this does not seem to be the case? Is this true?

Here's a bit of code to illustrate what I'm talking about:

--- Code: ---  Class1 = class
  strict private
    fStr : String;
    fInt : integer;
    function GetStr : String;
    function GetInt : Integer;
    procedure SetStr(const aValue : String);
    procedure SetInt(Const aValue : Integer);
  strict protected
    function MakeNewKey : String;
    property Str : String  read GetStr write SetStr;
    property Int : Integer read GetInt write SetInt;
  public
    constructor create;
  end;

  Class2 = class(class1)
  public
    property Str; //this works
    property Int; // this works
    function MakeNewKey; //this doesn't. It would seem that FPC requires
                                        //(a) the full function signature and
                                        //(b) its implementation even if does nothing more than calling inherited? Right?
  end;

--- End code ---

taazz:
I don't see it. what this suppose not to do?

EganSolo:
It's trying to redeclare the function MakeNewKey public in Class2 where it is strict protected. See how we can do that with properties? We can redeclare them public in class2 without having to reimplement them but it does not look like we can do the same with methods. In other words, even when we are simply changing the visibility of a method between a class and its descendant, we have to implement that method, even if we are not introducing any new behavior. Does this make sense?

I'm trying to confirm that this is case.

lainz:

--- Quote from: taazz on June 30, 2015, 12:50:57 am ---I don't see it. what this suppose not to do?

--- End quote ---


--- Code: ---function MakeNewKey; //this doesn't. It would seem that FPC requires
                                        //(a) the full function signature and
                                        //(b) its implementation even if does nothing more than calling inherited? Right?
--- End code ---

But I don't know the answer. He wants to know if he can do public an inherited protected function
--- Code: ---function MakeNewKey;
--- End code ---
with that code. Or if requires the full signature
--- Code: ---function MakeNewKey : String;
--- End code ---
.

I don't have FPC here to test.

taazz:
http://stackoverflow.com/questions/8232990/best-approach-to-elevate-method-visibility-in-a-descendant-class definite answer to your question. So no there is no mechanism that would allow you to change the visibility of a method as you can with properties.

Navigation

[0] Message Index

Go to full version