Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Method Overriding and Method Overloading?

Posted on 2004-04-19
6
Medium Priority
?
348 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 120 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Integration Management Part 2
Screencast - Getting to Know the Pipeline
Suggested Courses

885 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