Link to home
Start Free TrialLog in
Avatar of brimountain
brimountain

asked on

Method Overriding and Method Overloading?

Please Help

I am still very confused about method overriding and method overloading. Anyone here please help to clarify them.  Giving examples would be cool. I am a beginner

Thank you very much
Avatar of BJThomsen
BJThomsen

Method overriding is where you take a method from an ancestor class and change its parameters. For example:

TObject has a Method Create which has no input parameters. You derive a class from Tobject and want to add a parameter.

TMyObject = class(TObject)
public
  constructor Create(MyParam: string); override;
end;

Method overloading is a way to make one method name do dual duty depending on the paramaters given as you see below depending on if your input parameters are Integers or Reals the apporpriate method is called.

TMyObject = class(TObject)
public
  function Add( X, Y: Integer): Integer; overload;
  function Add( X, Y: Real): Real; overload;
end;

One thing you cannot do is overload a published function, because of run time info the below will not work.

TMyObject = class(TObject)
pubished
  function Add( X, Y: Integer): Integer; overload;
  function Add( X, Y: Real): Real; overload;
end;
lol Ignore the part about override. Got it mixed up with reintroduce.

Overridng a Methos is simply you want your class to have its own implemention of a method declaired in a base class.

Class1 = Class(TObject)
public
  procedure MyMethod( MyParam: String);
end;

Class2 = Class(Class1)
  procedure MyMethod( MyParam: String); override;
end;

Reintroduce is where you take a method from an ancestor class and change its parameters. For example:

TObject has a Method Create which has no input parameters. You derive a class from Tobject and want to add a parameter.

TMyObject = class(TObject)
public
  constructor Create(MyParam: string); reintroduce;  //This will hide the original Create
end;

TMyObject = class(TObject)
public
  constructor Create(MyParam: string); reintroduce; overload;  //This will not hide the original Create
end;

Avatar of brimountain

ASKER

Hi  BJThomsen

Thank you very much for your answer. They are very clear and I like the way you documented your reply. I think I completely understand overload method and main point of override method.  However, I am still have some points not sure:

1) I don't understand these sentences "// This will hide the original Create"  and  "// This will not hide the original Create". I need more explanation here.

2)When I read the book, I often see that if the method in the parent class is virtual then the method in the child class is override. Is there any thing special here?

Example:
TBase = class
   ...
   Procedure Show; virtual;
   ...
end;

TBase1 = class(TBase)
   ...
   Procedure Show; override;
   ...
end;
bri: your example is correct and BJ's is not

override let's you reimplement and/or reuse the inherited implementiation of a virtual or dynamic method - for example the Create of TComponent
overload let'y you have different implementations of the same method within a class or any descendant's it might have
an example would be the IntToStr function
Thank you, Lee Nover

BJThomsen, please continue
ASKER CERTIFIED SOLUTION
Avatar of BJThomsen
BJThomsen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial