C# Inheritance and Default Constructors

Do C# classes inherit constructors?

Constructors have never been inheritable in the entire lifetime of the C# language. That hasn't changed in C# 5.0: at the end of section 1.6.7.1 of the C# 5.0 spec, it still says:

Unlike other members, instance constructors are not inherited, and a class has no instance constructors other than those actually declared in the class. If no instance constructor is supplied for a class, then an empty one with no parameters is automatically provided.

So it still holds true today, and I imagine it will remain so in the foreseeable future.

Inheritance and base constructor

The basic answer to your question is yes. Your derived classes must define a constructor, specifically they must do so when no default constructor is available on the base class.

This is because base class constructors are always fired when derived classes are created (in fact, they are fired first). If you don't have a constructor to pass the base class constructor its required arguments, the compiler doesn't know how to handle this.

In your case, something like

public Dragon(string _name, int _health, int _attack, int _level)
:base(_name, _health, _attack, _level)
{
}

Will get you started. You may need (and can have) other parameters for your Dragon of course. You can also pass literals into the base constructor (so you don't need to parameterize the Dragon one with all base class arguments).

public Dragon(string _name)
:base(_name, 1000, 500, 10)
{
}

The only requirement is that an existing base class constructor is used.

Can I inherit constructors?

You don't need to create loads of constructors, all with the same code; you create only one, but have the derived classes call the base constructor:

public class Base
{
public Base(Parameter p)
{
Init(p)
}

void Init(Parameter p)
{
// common initialisation code
}
}

public class Derived : Base
{
public Derived(Parameter p) : base(p)
{

}
}

C# how to inherit from default constructor

You can do it like this:

public AuctionVehicle(int AuctionID) : this() 
{
...
}

Inheritance with base class constructor with parameters

The problem is that the base class foo has no parameterless constructor. So you must call constructor of the base class with parameters from constructor of the derived class:

public bar(int a, int b) : base(a, b)
{
c = a * b;
}

How to inherit constructors?

Yes, you will have to implement the constructors that make sense for each derivation and then use the base keyword to direct that constructor to the appropriate base class or the this keyword to direct a constructor to another constructor in the same class.

If the compiler made assumptions about inheriting constructors, we wouldn't be able to properly determine how our objects were instantiated. In the most part, you should consider why you have so many constructors and consider reducing them to only one or two in the base class. The derived classes can then mask out some of them using constant values like null and only expose the necessary ones through their constructors.

Update

In C#4 you could specify default parameter values and use named parameters to make a single constructor support multiple argument configurations rather than having one constructor per configuration.

Constructor Parameters and Inheritance

As to why you can't do:

public SubClass(string SubString) : base(BaseString)

What would BaseString be?

You could do:

public SubClass(string SubString) : base("SomeFixedString")

or

public SubClass(string SubString) : base(SubString)

But if you want to pass one string to the base class constructor's parameter and have an additional one, you'll need to accept two parameters.

As to keeping the same name, you don't. You could do:

public SubClass(string AnotherString, string SubString) : base(AnotherString)

As to the last question, the first parameter isn't doing nothing, it's being passed to the base class constructor. You could use it for something else if you wanted to.

Inheritence problem in C# without calling base class constructor

As everybody else has pointed out, here:

Shape s1 = new Shape();

You're initializing your Shape class. You think it doesn't make sense because you don't have a Constructor defined, but since you don't have it it has been dynamicaly generated so the program doesn't break.
Thus, your Shaple class is executed like this:

class Shape
{
public Shape()
{
}

public void Area()
{
Console.WriteLine("I am a shape");
}
}


Related Topics



Leave a reply



Submit