Solved

Method Overriding and Method Overloading?

Posted on 2004-04-19
6
343 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:brimountain
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:BJThomsen
ID: 10864538
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;
0
 
LVL 3

Expert Comment

by:BJThomsen
ID: 10864575
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;

0
 

Author Comment

by:brimountain
ID: 10865787
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;
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 12

Expert Comment

by:Lee_Nover
ID: 10865903
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
0
 

Author Comment

by:brimountain
ID: 10872028
Thank you, Lee Nover

BJThomsen, please continue
0
 
LVL 3

Accepted Solution

by:
BJThomsen earned 40 total points
ID: 10875596
You and Lee are correct I did forget the virtual in my example;

The virtual lets the compiler know that its ok to override the function and is expected.

What I mean by "hide the original Create" in the reintroduce example is this.

In the example below

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

You can create an instance of TMyObject like:

MyObject := TMyObject.Create  <--  This uses the create method from the base class TObject.

or you could use

Myobject := TMyobject.Create('This is a string'); <-- This uses the create method from the TMyObject

If you do not use the overload
  constructor Create(MyParam: string); reintroduce;

You cannot use the MyObject := TMyObject.Create from TObject because it is hidden from use by TMyObjects create method.

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question