PC Review


Reply
Thread Tools Rate Thread

Can we override constructor

 
 
ad
Guest
Posts: n/a
 
      2nd Jan 2005
When a class (say class1) inherited form another class (Class0).

The constructor in the class0 will be executing before the constructor of
class1.

Can we over the constructor?


 
Reply With Quote
 
 
 
 
Amit
Guest
Posts: n/a
 
      2nd Jan 2005
Depends on what kind of constructor it is - if it is the default constructor
(i.e. constructor with no parameters) then you can't override the parent's
constructor, it will always execute. If it is a non-default constructor then
it is automatically overriden unless you call it using the "base" keyword.
Note that the default constructor of the parent is always called regardless
of what kind of constructor is being invoked of the child class. Also,
whenever a base-class constructor executes, it does so before the inherited
class constructor is run.
Hope that helps,
~amit

"ad" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> When a class (say class1) inherited form another class (Class0).
>
> The constructor in the class0 will be executing before the constructor of
> class1.
>
> Can we over the constructor?
>
>



 
Reply With Quote
 
Jon Shemitz
Guest
Posts: n/a
 
      2nd Jan 2005
Amit wrote:

> Note that the default constructor of the parent is always called regardless
> of what kind of constructor is being invoked of the child class.


No, this is not true. You can specify which base constructor will be
called -

public ThisType(t1 p1, t2 p2): base(p1) { /*...*/ }

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
 
Reply With Quote
 
Amit
Guest
Posts: n/a
 
      2nd Jan 2005
oops you're right... the default constructor of the parent class is always
called IF no other constructor is specified.


"Jon Shemitz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Amit wrote:
>
> > Note that the default constructor of the parent is always called

regardless
> > of what kind of constructor is being invoked of the child class.

>
> No, this is not true. You can specify which base constructor will be
> called -
>
> public ThisType(t1 p1, t2 p2): base(p1) { /*...*/ }
>
> --
>
> programmer, author http://www.midnightbeach.com
> and father http://www.midnightbeach.com/hs



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      2nd Jan 2005
ad <(E-Mail Removed)> wrote:
> When a class (say class1) inherited form another class (Class0).
>
> The constructor in the class0 will be executing before the constructor of
> class1.
>
> Can we over the constructor?


You can't override constructors, because they aren't inherited. It's
not entirely clear what you're after, but the following page may help:

http://www.pobox.com/~skeet/csharp/constructors.html

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Jon Shemitz
Guest
Posts: n/a
 
      2nd Jan 2005
Amit wrote:

> oops you're right... the default constructor of the parent class is always
> called IF no other constructor is specified.


Yes, this is better. A base constructor will always be called, for
every class that descends from object, because every class must have
at least one constructor that calls a base() constructor (explicitly
or implicitly) and every call to a this() constructor must ultimately
call a base() constructor.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
 
Reply With Quote
 
James Curran
Guest
Posts: n/a
 
      3rd Jan 2005
"Jon Shemitz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> A base constructor will always be called, for
> every class that descends from object, because every class must have
> at least one constructor that calls a base() constructor (explicitly
> or implicitly)


And because, one way or another, the base object must be constructed.

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com




 
Reply With Quote
 
Jon Shemitz
Guest
Posts: n/a
 
      3rd Jan 2005
James Curran wrote:
>
> "Jon Shemitz" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> > A base constructor will always be called, for
> > every class that descends from object, because every class must have
> > at least one constructor that calls a base() constructor (explicitly
> > or implicitly)

>
> And because, one way or another, the base object must be constructed.


Yes. That's the semantic reason for the syntactic requirement.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Okay to override an ASP.NET page's constructor slolife Microsoft ASP .NET 2 14th May 2007 10:39 PM
override default values in a constructor TJS Microsoft ASP .NET 4 12th Jun 2005 08:24 PM
Calling constructor override after doing some checks =?Utf-8?B?Sm9zZWYgTWVpbGU=?= Microsoft C# .NET 2 9th Mar 2005 04:19 PM
Calling a struct constructor in a class constructor body Karl M Microsoft VC .NET 4 19th Dec 2004 01:21 PM
Calling overloaded constructor with values inside another constructor Tristan Microsoft C# .NET 2 30th Mar 2004 01:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:37 PM.