Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically making Base Constructors available in derived class?

Tags:

I have a Base Class with two constructors, requiring a parameter:

public abstract class StoreBase  {     private readonly SomeObject_sobj;      protected StoreBase(SomeObject sobj)     {         _sobj = sobj;     }      protected StoreBase(OtherObject oobj)     {         _sobj = new SomeObject(oobj);     } } 

Then I have a derived class:

public class MyDerived: StoreBase {  } 

This causes a compilation error as base class doesn't contain parameterless constructor.

My understanding is that because MyDerived doesn't contain a constructor, the compiler adds a parameterless constructor (that's well known and nothing to do with derived classes). However, as it derives from another class, the base class constructor needs to run first, and there is no way to determine which constructor should run from the empty MyDerived constructor.

Basically I'm asking: can I avoid copy/pasting all constructors from Base into Derived class if I really don't need additional constructor logic? Can I say "Take all constructors from base" without adding them all?

(And yes, I know I could/should refactor this into a parameterless constructor and a protected virtual Initialize() method. but I still wonder if I can work with constructors and still avoid copy/paste)

like image 497
Michael Stum Avatar asked Jan 19 '10 18:01

Michael Stum


People also ask

Does base class constructor get called automatically?

If a base class has a default constructor, i.e., a constructor with no arguments, then that constructor is automatically called when a derived class is instantiated if the derived class has its own default constructor.

How can a base class constructor be used as a derived class?

How to call the parameterized constructor of base class in derived class constructor? To call the parameterized constructor of base class when derived class's parameterized constructor is called, you have to explicitly specify the base class's parameterized constructor in derived class as shown in below program: C++

Are base class constructors inherited by derived classes?

In inheritance, the derived class inherits all the members(fields, methods) of the base class, but derived class cannot inherit the constructor of the base class because constructors are not the members of the class.

Which constructor is called automatically?

Static constructors are called automatically, immediately before any static fields are accessed, and are generally used to initialize static class members.


1 Answers

No - you will need to implement the (appropriate) constructors in the derived class, as well.

The derived class only needs to use one of the base constructors - so the constructors required in it may be completely different than the base class. They will need to be implemented by hand, even if that's just:

public class MyDerived : StoreBase {      public MyDerived(SomeObject sobj) : base(sobj) {}      public MyDerived(OtherObject  oobj) : base(oobj) {} } 

Also:

(And yes, I know I could/should refactor this into a parameterless constructor and a protected virtual Initialize() method. but I still wonder if I can work with constructors and still avoid copy/paste)

Although I see this touted, I believe this is not always a good practice. In many cases, this is actually problematic, as you're relying on the subclass to properly call Initialize if they override your protected virtual method. For example, if the subclass did this, it could potentially be very bad:

public class MyDerived : StoreBase {    // .. other stuff    protected override void Initialize()    {        // Leave out, intentionally or accidentally, the following:        // base.Initialize();     } } 

I actually avoid this in most situations, and initialize in the constructors (or in a private, non-virtual initialize method). Not doing this breaks any guarantees you have that your initialization will always occur the way you intend.

Constructors and constructor chaining provide the same functionality, with much better guarantees.

like image 90
Reed Copsey Avatar answered Oct 12 '22 18:10

Reed Copsey