Solved

Method Overriding and Method Overloading?

Posted on 2004-04-19
6
338 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now