Recent

Author Topic: Please confirm this can't be done...  (Read 2912 times)

EganSolo

  • Full Member
  • ***
  • Posts: 202
Please confirm this can't be done...
« on: June 30, 2015, 12:40:00 am »
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: [Select]
  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;

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: Please confirm this can't be done...
« Reply #1 on: June 30, 2015, 12:50:57 am »
I don't see it. what this suppose not to do?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

EganSolo

  • Full Member
  • ***
  • Posts: 202
Re: Please confirm this can't be done...
« Reply #2 on: June 30, 2015, 01:00:11 am »
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

  • Hero Member
  • *****
  • Posts: 3965
  • Leandro Diaz
Re: Please confirm this can't be done...
« Reply #3 on: June 30, 2015, 01:02:30 am »
I don't see it. what this suppose not to do?

Code: [Select]
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?

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

I don't have FPC here to test.
https://lainz.github.io/ - My Website :)
https://lazpaint.github.io/ -  Download LazPaint

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: Please confirm this can't be done...
« Reply #4 on: June 30, 2015, 01:33:13 am »
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.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018