471,316 Members | 946 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,316 software developers and data experts.

overload constructor

Hi

I am working through a learning module privately where there is discussion
about overloading the constructor.

Could someone please let me know the benefit or purpose of doing this in a
class?

Thanks

doug
Dec 2 '05 #1
7 2277
Doug wrote:
I am working through a learning module privately where there is discussion
about overloading the constructor.

Could someone please let me know the benefit or purpose of doing this in a
class?


Sure - often you don't want to specify the same parameters every time
you construct an object. For instance, StreamWriter has several
overloads - some take a filename, some take a Stream.

Alternatively, look at ArrayList - you don't always want to specify the
initial capacity, but sometimes you do. Removing either of those
constructors would make life harder or less efficient.

Does that help?

Jon

Dec 2 '05 #2
Thanks Jon

So the purpose of the constructor is to create an instance of an object and
therefore allow access to methods that are contained within that object,
And overloading would allow different parameters to be provided to the
method of different instances of that class??
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Doug wrote:
I am working through a learning module privately where there is
discussion
about overloading the constructor.

Could someone please let me know the benefit or purpose of doing this in
a
class?


Sure - often you don't want to specify the same parameters every time
you construct an object. For instance, StreamWriter has several
overloads - some take a filename, some take a Stream.

Alternatively, look at ArrayList - you don't always want to specify the
initial capacity, but sometimes you do. Removing either of those
constructors would make life harder or less efficient.

Does that help?

Jon

Dec 3 '05 #3
Hi Doug,

Doug wrote:
And overloading would allow different parameters to be provided to the
method of different instances of that class??

This sentence appears difficult to understand.
The constructor is a special method used to initialize the instance.
The parameters of the constructor are used to initialize the object.
Sometimes you wish to specify more values; sometimes you want to
specify less and let the object uses defaults; sometimes you could only
specify some at the time of instantiation, and will set some properties
later. Constructor overloading makes life easier by provide you with
many choices to initialize the object at the time it is created.

Thi

Dec 3 '05 #4
Thanks Thi

Being still at the learning gate of this language I like to get clarity
about the various elements. Most of the books I have read or are reading
about C# seem to assume that the readers understand these concepts and
therefore they skim over a detailed explanation.

Do you know of any website links that cover these concepts? I struggled
with 'state' for a while because the material that I was reading assumed it
was obvious - maybe not to me.

Thanks again to you and Jon.

Doug
"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi Doug,

Doug wrote:
And overloading would allow different parameters to be provided to the
method of different instances of that class??

This sentence appears difficult to understand.
The constructor is a special method used to initialize the instance.
The parameters of the constructor are used to initialize the object.
Sometimes you wish to specify more values; sometimes you want to
specify less and let the object uses defaults; sometimes you could only
specify some at the time of instantiation, and will set some properties
later. Constructor overloading makes life easier by provide you with
many choices to initialize the object at the time it is created.

Thi

Dec 3 '05 #5
gordon <go**********@optusnet.com.au> wrote:
So the purpose of the constructor is to create an instance of an object and
therefore allow access to methods that are contained within that object,
Yes.
And overloading would allow different parameters to be provided to the
method of different instances of that class??


No - the parameters which are provided to the method come later, and
are independent of what you've provided in the constructor. Usually the
different parameters in the constructor allow the object to be created
with different "state" information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 3 '05 #6
>Most of the books I have read or are reading
about C# seem to assume that the readers understand these concepts and
therefore they skim over a detailed explanation.

They are object-oriented concepts. You'll be familiar with them soon,
don't worry. Just try some tutorials.

Dec 5 '05 #7
Hi Gordon,

Although part of the Vava VM tutorial this link provides some good stuff
on OO topics. Where there are code examples, they are in Java which is
very similar to C#. However, the main thrust is the conceptual side of OO.

http://java.sun.com/docs/books/tutorial/java/concepts/

Cheers

Simon

gordon wrote:
Thanks Thi

Being still at the learning gate of this language I like to get clarity
about the various elements. Most of the books I have read or are reading
about C# seem to assume that the readers understand these concepts and
therefore they skim over a detailed explanation.

Do you know of any website links that cover these concepts? I struggled
with 'state' for a while because the material that I was reading assumed it
was obvious - maybe not to me.

Thanks again to you and Jon.

Doug
"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi Doug,

Doug wrote:
And overloading would allow different parameters to be provided to the
method of different instances of that class??


This sentence appears difficult to understand.
The constructor is a special method used to initialize the instance.
The parameters of the constructor are used to initialize the object.
Sometimes you wish to specify more values; sometimes you want to
specify less and let the object uses defaults; sometimes you could only
specify some at the time of instantiation, and will set some properties
later. Constructor overloading makes life easier by provide you with
many choices to initialize the object at the time it is created.

Thi


Dec 5 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Nimmi Srivastav | last post: by
4 posts views Thread by Chiller | last post: by
7 posts views Thread by Sean | last post: by
10 posts views Thread by Benny Raymond | last post: by
2 posts views Thread by Tom | last post: by
7 posts views Thread by portroe | last post: by
3 posts views Thread by needin4mation | last post: by
1 post views Thread by fabian.lim | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.