473,383 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Can we override constructor

ad
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?
Nov 16 '05 #1
7 37739
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" <ad@wfes.tcc.edu.tw> wrote in message
news:uJ**************@TK2MSFTNGP09.phx.gbl...
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?

Nov 16 '05 #2
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
Nov 16 '05 #3
oops you're right... the default constructor of the parent class is always
called IF no other constructor is specified.
"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:41***************@midnightbeach.com...
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

Nov 16 '05 #4
ad <ad@wfes.tcc.edu.tw> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
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
Nov 16 '05 #6
"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:41***************@midnightbeach.com...
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


Nov 16 '05 #7
James Curran wrote:

"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:41***************@midnightbeach.com...
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
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
2
by: Josef Meile | last post by:
Hi, I have this constructor: public CExcelDatabase(string host, string user, string password, string database, bool promptCredentials, int findExcelInstance, bool readOnly) { //Some code...
4
by: TJS | last post by:
How can I override the default values, of a class constructor , from an ASP.net page ? Public Sub New( ) HeaderStyle.BackColor = ColorTranslator.FromHtml("#5C85AD") HeaderStyle.ForeColor =...
11
by: RickHodder | last post by:
I'm having a problem, and here is a simplified example of code that demonstrates it: public class BizObj { public string TableName=""; private DataSet oData; public BizObj()
23
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
2
by: slolife | last post by:
See any problem with this code: Here's my Base page class: Public Class BasePage Inherits System.Web.UI.Page Protected m_NeedAlerts As Boolean Private Sub Page_Init(ByVal sender As...
34
by: Smithers | last post by:
I have some logic that populates UI controls with values from App.config. A couple of checkboxes get checked or unchecked; and items loaded into a checked list box. Two reasonable places to put...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.