Recent

Author Topic: [SOLVED]Inheritance question...  (Read 974 times)

cdbc

  • Hero Member
  • *****
  • Posts: 1951
    • http://www.cdbc.dk
[SOLVED]Inheritance question...
« on: August 28, 2022, 11:57:35 am »
Hi
Quick question, can one override an already overridden method?:
Code: Pascal  [Select][+][-]
  1. class1 = class
  2.   method1; virtual;
  3. end;
  4. class2 = class(class1)
  5.   method1; override;
  6. end;
  7. class3 = class(class2)
  8.   method1; override;  <----- is this possible?!?
  9. end;
  10.  
If one declares class2.method1 as virtual, can you still call inherited method1; in it? The compiler warns me that it hides the ancestral method.
A little help would be appreciated.
Regards Benny
« Last Edit: August 29, 2022, 08:43:46 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Thaddy

  • Hero Member
  • *****
  • Posts: 16652
  • Kallstadt seems a good place to evict Trump to.
Re: Inheritance question...
« Reply #1 on: August 28, 2022, 12:12:04 pm »
Class3.Method1 should call inherited method1 (class2) at the first line of the class3.method1 override.
Then class2.method1 will be called first and subsequently the Class3.Method1 can add additional code.
But I am sure they don't want the Trumps back...

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Inheritance question...
« Reply #2 on: August 28, 2022, 12:18:02 pm »
Hi
Quick question, can one override an already overridden method?:
Code: Pascal  [Select][+][-]
  1. class1 = class
  2.   method1; virtual;
  3. end;
  4. class2 = class(class1)
  5.   method1; override;
  6. end;
  7. class3 = class(class2)
  8.   method1; override;  <----- is this possible?!?
  9. end;
  10.  
If one declares class2.method1 as virtual, can you still call inherited method1; in it? The compiler warns me that it hides the ancestral method.
A little help would be appreciated.
Regards Benny
Yes it is possible, there should be no error or warning.

Thaddy

  • Hero Member
  • *****
  • Posts: 16652
  • Kallstadt seems a good place to evict Trump to.
Re: Inheritance question...
« Reply #3 on: August 28, 2022, 12:20:15 pm »
E.G:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. type
  3. class1 = class
  4.   procedure method1; virtual;
  5. end;
  6. class2 = class(class1)
  7.   procedure method1; override;
  8. end;
  9. class3 = class(class2)
  10.   procedure method1; override; // <----- is this possible?!?
  11. end;
  12. procedure class1.method1;
  13. begin
  14.   writeln('Class1');
  15. end;
  16. procedure class2.method1;
  17. begin
  18.   inherited method1;
  19.   writeln('Class2');
  20. end;
  21. procedure class3.method1;
  22. begin
  23.   inherited method1;
  24.   writeln('Class3');
  25. end;
  26.  
  27. var a:Class1;
  28.     b:Class2;
  29.     c:Class3;
  30. begin
  31.   a:= Class1.Create;
  32.   b:= Class2.Create;
  33.   c:= Class3.Create;
  34.   try
  35.     c.method1;
  36.   finally
  37.     c.free;
  38.     b.free;
  39.     a.free;
  40.   end;
  41.   readln;
  42. end.
But I am sure they don't want the Trumps back...

Thaddy

  • Hero Member
  • *****
  • Posts: 16652
  • Kallstadt seems a good place to evict Trump to.
Re: Inheritance question...
« Reply #4 on: August 28, 2022, 12:21:39 pm »
Yes it is possible, there should be no error or warning.
But not his way: the warning appears when you do not call inherited!! See my example, that is warning free.
If you comment out the inherited calls the warning appears.
But I am sure they don't want the Trumps back...

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: Inheritance question...
« Reply #5 on: August 28, 2022, 12:22:27 pm »
Yes it is possible, there should be no error or warning.
note: example show <> question ask.

Quote
If one declares class2.method1 as virtual, can you still call inherited method1; in it? The compiler warns me that it hides the ancestral method.
fpc:
Code: [Select]
classy.pas(12,15) Warning: An inherited method is hidden by "method1;"
Answer: no. read documentation https://www.freepascal.org/docs-html/ref/refsu27.html
Quote
The compiler will compile it, but using Inherited can produce strange effects.

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Inheritance question...
« Reply #6 on: August 28, 2022, 12:23:25 pm »
Yes it is possible, there should be no error or warning.
But not his way: the warning appears when you do not call inherited!! See my example, that is warning free.
tested it on lazarus 2.2.2 with and with out calling inherited no warning at both cases.

Thaddy

  • Hero Member
  • *****
  • Posts: 16652
  • Kallstadt seems a good place to evict Trump to.
Re: Inheritance question...
« Reply #7 on: August 28, 2022, 12:26:26 pm »
The question was if you could still execute class2.method1 in Class3.method1. The answer is yes, as per my example.
But I am sure they don't want the Trumps back...

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: Inheritance question...
« Reply #8 on: August 28, 2022, 12:42:24 pm »
tested it on lazarus 2.2.2 with and with out calling inherited no warning at both cases.
question ask: use virtual class 2
Code: Pascal  [Select][+][-]
  1. program classy;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$define questionmakewarning}
  5.  
  6. type
  7.   TClassA=class
  8.     procedure method1;virtual;
  9.   end;
  10.  
  11.   TClassB=class(TClassA)
  12.     {$ifdef questionmakewarning}
  13.     procedure method1;virtual;
  14.     {$else}
  15.     procedure method1;override;
  16.     {$endif}
  17.   end;
  18.  
  19.   TClassC=class(TClassB)
  20.     procedure method1;override;
  21.   end;
  22.  
  23.  
  24. procedure TClassA.method1;
  25. begin
  26.   writeln('ClassA.method1');
  27. end;
  28.  
  29. procedure TClassB.method1;
  30. begin
  31.   inherited;
  32.   writeln('ClassB.method1');
  33. end;
  34.  
  35. procedure TClassC.method1;
  36. begin
  37.   inherited;
  38.   writeln('ClassC.method1');
  39. end;
  40.  
  41.  
  42. var
  43.   ClassA:TClassA;
  44.   ClassB:TClassB;
  45.   ClassC:TClassC;
  46. begin
  47.   ClassA:=TClassA.Create;
  48.   ClassB:=TClassB.Create;
  49.   ClassC:=TClassC.Create;
  50.  
  51.   writeln(1);
  52.   ClassA.Method1;
  53.   writeln(2);
  54.   ClassB.Method1;
  55.   writeln(3);
  56.   ClassC.Method1;
  57.  
  58.   ClassC.Free;
  59.   ClassB.Free;
  60.   ClassA.Free;
  61. end.
  62.  
then warning:
Code: [Select]
fpc -B classy.pas
Free Pascal Compiler version 3.2.2 [2021/05/16] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling classy.pas
classy.pas(13,15) Warning: An inherited method is hidden by "method1;"
Linking classy
61 lines compiled, 0.1 sec
1 warning(s) issued
Maybe have warning off ?

Thaddy

  • Hero Member
  • *****
  • Posts: 16652
  • Kallstadt seems a good place to evict Trump to.
Re: Inheritance question...
« Reply #9 on: August 28, 2022, 12:45:19 pm »
No, but if you forget override or make it virtual again without calling inherited, then you get the warning.
I still think you interpreted the question wrong. (I always have warnings on unless they are bogus. Then I switch them off on an individual basis)
« Last Edit: August 28, 2022, 12:47:31 pm by Thaddy »
But I am sure they don't want the Trumps back...

PascalDragon

  • Hero Member
  • *****
  • Posts: 5899
  • Compiler Developer
Re: Inheritance question...
« Reply #10 on: August 28, 2022, 06:05:44 pm »
If one declares class2.method1 as virtual, can you still call inherited method1; in it? The compiler warns me that it hides the ancestral method.
A little help would be appreciated.

If you declare class2.method1 as virtual you're starting a new method chain, which is why the compiler will warn you that class1.method1 is hidden. A method declared with override is implicitly virtual as well (assuming there is anything to override in the first place, otherwise you're not allowed to use override anyway and must use virtual instead). Use reintroduce in addition to virtual to really go through with the new method chain (and thus suppressing the warning).

In any case you can still call inherited method1 from class2.method1 to indeed call class1.method1.

The difference will be if you have a variable of type class1 and call method1 then in case of a new method chain the compiler will call class1.method1 (cause that isn't overridden) and in case of a continued method chain it will call class2.method1 or class3.method1 depending on what you instantiated it as.

Class3.Method1 should call inherited method1 (class2) at the first line of the class3.method1 override.
Then class2.method1 will be called first and subsequently the Class3.Method1 can add additional code.

Using inherited is highly dependant on the use case and the code in question and is in no way required by the language itself.

Yes it is possible, there should be no error or warning.
But not his way: the warning appears when you do not call inherited!! See my example, that is warning free.
If you comment out the inherited calls the warning appears.

No, the compiler issues the warning once it has parsed the declaration. It couldn't care less about the implementation at that point and thus doesn't know anything about you using inherited. The warning appears because a new method chain is introduced with the class2.method1 being declared as virtual and the solution is either to declare it as override (to continue the method chain started by class1.method1) or to declare it as reintroduce as well to suppress the warning (and thus starting a new method chain).


cdbc

  • Hero Member
  • *****
  • Posts: 1951
    • http://www.cdbc.dk
Re: Inheritance question...
« Reply #11 on: August 29, 2022, 08:43:13 am »
Hi
Thank you all for your quick answers, they hit the spot. I' was coding along happy, when suddenly i got in doubt ?!? ... about things i've known for years, and then apparently forgot  %)  :-X
@PascalDragon: Thank you for the explanation, that got my memory working again.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

 

TinyPortal © 2005-2018