Recent

Author Topic: [CLOSED] Any difference between inherited; and inherited MethodName; ?  (Read 1044 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Hi,

As the title implies, are there any differences between just "inherited;" and "inherited MethodName;"? 
Following is the case.

Code: Pascal  [Select][+][-]
  1. TClass1 = class (TStringList)
  2.     procedure Update; virtual;
  3. end;
  4.  
  5. TClass2 = class(TClass1);
  6.     a : integer;
  7.     procedure Update; override;
  8. end;
  9.  
  10. // and in the implementation section
  11.  
  12. procedure TClass1.Update;
  13. begin
  14.      DoSomething (Self);    // Do something with the TStringList content
  15. end;
  16.  
  17. // But following two procedures produce different results
  18.  
  19. procedure TClass2.Update;
  20. begin
  21.      a := 1;
  22.      inherited;   // this seems to operate on the TClass1
  23. end;
  24.  
  25. procedure TClass2.Update;
  26. begin
  27.      a := 1;
  28.      inherited Update;  // This works correctly.
  29. end;
  30.  

Are there any difference between just writing inherited and inherited Update?   
« Last Edit: February 24, 2020, 01:01:58 pm by egsuh »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Any difference between inherited; and inherited MethodName; ?
« Reply #1 on: February 24, 2020, 10:26:34 am »
There should be no difference at all; both should do exactly the same.

Can you provide a more complete example of what you're doing and what differences you're seeing?
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Any difference between inherited; and inherited MethodName; ?
« Reply #2 on: February 24, 2020, 10:40:04 am »
There should be no difference at all; both should do exactly the same.
That is not correct. Inherited calls an inherited parameterless method with exactly the same signature. Inherited with a specifier or parameter list can call - any - method in the ancestor and with different signatures.
So there is a huge difference.

Calling inherited plain is most useful for constructors since default (parameterless) constructors can not be virtual. It can also be useful in different scenario's.
Demo:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}{$H+}
  2. type
  3.   TAncestor = class
  4.   public
  5.     procedure Foo;
  6.     constructor create;
  7.   end;
  8.  
  9.   TMyClass = class(TAncestor)
  10.   public
  11.     constructor create;
  12.     procedure Bar;
  13.   end;
  14.  
  15.   constructor TAncestor.Create;
  16.   begin
  17.     writeln('Ancestor');
  18.   end;
  19.  
  20.   procedure TAncestor.Foo;
  21.   begin
  22.     writeln('Foo');
  23.   end;
  24.  
  25.   constructor TMyClass.Create;
  26.   begin
  27.     inherited;
  28.     writeln('TMyClass');
  29.   end;
  30.  
  31.   procedure TMyClass.Bar;
  32.   begin
  33.     inherited Foo;
  34.     writeln('Bar');
  35.   end;
  36. var
  37.   a,b:TAncestor;  // same root class
  38. begin
  39.   a := TAncestor.Create;
  40.   a.foo;
  41.   b := TMyClass.Create;
  42.   (b as TMyClass).bar; // soft cast
  43.   b.free;
  44.   a.free;
  45. end.
« Last Edit: February 24, 2020, 10:53:45 am by Thaddy »
Specialize a type, not a var.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Any difference between inherited; and inherited MethodName; ?
« Reply #3 on: February 24, 2020, 10:50:33 am »
Yes, Thaddy, but I'm talking in the context of the OP's code: in his case inherited; and inherited Update; inside an overriden Update should do the same.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Any difference between inherited; and inherited MethodName; ?
« Reply #4 on: February 24, 2020, 10:54:11 am »
Added example for OP.
Specialize a type, not a var.

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Any difference between inherited; and inherited MethodName; ?
« Reply #5 on: February 24, 2020, 10:56:16 am »
There should be no difference at all; both should do exactly the same.
That is not correct. Inherited calls an inherited parameterless method with exactly the same signature. Inherited with a specifier or parameter list can call - any - method in the ancestor and with different signatures.
So there is a huge difference.
I disagree, at first there was no support for inherited with out a method name. I remember a semi heated discussion on the borland's newsgroup about introducing a new construct to allow the call of a parent's window message method if there was one declared in a parent control or no call at all if there was none and borland introduced the inherited with out a method name to accommodate for easier subclassing of existing controls. So there is a difference between inherited and inherited <method name> as far as I remember in the first case if the method does not exist no error should be raised and the call should be edited out while on the second case where the method name is defined if it does not exists an error should be raised, this is to accommodate any future development on the parent control with the knowledge or need for attention on the child.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9791
  • Debugger - SynEdit - and more
    • wiki
Re: Any difference between inherited; and inherited MethodName; ?
« Reply #6 on: February 24, 2020, 10:56:25 am »
As the title implies, are there any differences between just "inherited;" and "inherited MethodName;"? 

Yes, there is a difference.

First (as pointed out by others), you need to understand that you can use "inherited name(...)" to call any method of the parent class (including those that the parent class has inherited).
- You can call "inherited Foo", even if you call from a method that is not called "Foo".
- You can use "inherited name(...)" in any method. Even in a method that is NOT virtual/overridden.


Second, "inherited" without any name or function, is NOT identical to "inherited CurrentFunctionName(ArgsFromCurrentFunc)"
You can call "inherited" even if the parent class has no function of the same name. In that case the "inherited" is silently ignored.


If the parent class has (either by itself or inherited) a method of the same name (and params), then "inherited" is a shortcut for "inherited CurrentFunctionName(ArgsFromCurrentFunc)". And it will do the same thing.

However if you remove/rename the method in the parent, and you used the short "inherited", then you get no error if you forget to update the child class.
« Last Edit: February 24, 2020, 10:58:18 am by Martin_fr »

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Any difference between inherited; and inherited MethodName; ?
« Reply #7 on: February 24, 2020, 12:54:52 pm »
I disagree,
Feel free to disagree, but this has been the case since Delphi 1, which differs a great deal from TurboPascal.
As Martin added to and corrected (tnx!) my initial response, there is a difference and the difference is significant.

The discussion you refer to was afair merely a paradigm shift that was not immediately understood by the community.
« Last Edit: February 24, 2020, 01:04:57 pm by Thaddy »
Specialize a type, not a var.

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Any difference between inherited; and inherited MethodName; ?
« Reply #8 on: February 24, 2020, 01:01:37 pm »
Sorry, I cannot reproduce this now. 

Actually Update method calls an interfaced method, which sends text (TStringList.CommaText) through synapse to webserver.
Webserver restores the TStringList from the commatext, and does something with it.

At first, the content was not reproduced correctly with "inherited;" only(some lines missing), so I modified it to "inherited Update;" and the content was reproduced correctly.

But the codes were not executed correctly because there was another bug. I corrected the bug, and everything went well. 

For now, I'll tag this as closed and make a new thread if I encounter the same problem again.

 

TinyPortal © 2005-2018