Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Do we need constructors in an inheriting class?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
K Viltersten - 20 Mar 2008 21:32 GMT
I have:

 abstract class Vehicle
 {
   public Vehicle (int i) {...}
   abstract public void doMove ();
 }

When i write the following:

 class Car: Vehicle

magic happens and by pressing a key
combo, VS asks me if i want to extend
the abstract class.

I say, YES! And there it is - a stub
of a function already with "override"
and "throw this and that".

However, no constructor(s) is/are
created for me. Does it mean i need
not to implement those? In C++ i
needed to provide my own and make
them call the super class...

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Peter Duniho - 20 Mar 2008 21:41 GMT
> [...]
> However, no constructor(s) is/are created for me. Does it mean i need  
> not to implement those? In C++ i needed to provide my own and make them  
> call the super class...

Again, I think "just try it" is useful advice here.

But, no...just because the constructors weren't implemented for you, that  
doesn't mean you don't have to implement them.  The "extend the abstract  
class" applies only to the abstract members.  The constructor in the  
abstract class isn't itself abstract (nor could it be), and so wouldn't  
show up as part of the auto-generated members in your derived class.

If the abstract class had defined a constructor with no parameters, then  
you could get away with not defining any new constructors for the derived  
class.  But it doesn't, so you can't (and in fact, you'll be required to  
call the base constructor when declaring your derived constructor).

Pete
Ben Voigt [C++ MVP] - 21 Mar 2008 18:47 GMT
> However, no constructor(s) is/are
> created for me. Does it mean i need
> not to implement those? In C++ i
> needed to provide my own and make
> them call the super class...

If you want the same set of constructors... but often a derived class will
require additional parameters or supply defaults, so there's really no way
for the IDE to create the constructors automatically.
K Viltersten - 21 Mar 2008 23:57 GMT
>> However, no constructor(s) is/are
>> created for me. Does it mean i need
[quoted text clipped - 7 lines]
> so there's really no way for the IDE to
> create the constructors automatically.

Informative remark. Didn't think of it.
Thank you.

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Arne Vajhøj - 30 Mar 2008 01:03 GMT
>> However, no constructor(s) is/are
>> created for me. Does it mean i need
[quoted text clipped - 5 lines]
> require additional parameters or supply defaults, so there's really no way
> for the IDE to create the constructors automatically.

There should be an option to generate constructors with same arguments
as the super class.

There is a decent chance that is what is needed.

Arne

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.