Solved

Method Overriding and Method Overloading?

Posted on 2004-04-19
6
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Mdi application Child forms get behind control 7 361
Magic Software info 18 154
How to debug For loops? 3 74
Multi-layered image in FireMonkey 9 93
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 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