user393267
user393267

Reputation:

Correct way to invoke constructor of a parent class from the child

I have a class that inherit from another; the parent class has all the stats (imagine a RPG character sheet), while the child class has only few extra parameters.

When I start a child class, how do I invoke the constructor of the parent, to get all the parameter initialized with generic data? Do I have to call it explicitly or C# does it automatically?

parent class:

public class genericguy
{
    private string _name;
    private int _age;
    private string _phone;

    public genericguy(string name)
    {
        this._name = name;
        this._age = 18;
        this._phone = "123-456-7890";
    }
    // the rest of the class....
}

children class:

public class specificguy:genericguy
{
    private string _job;
    private string _address;

    public specificguy(string name)
    {
        this._name = name;
        this._job = "unemployed";
        this._address = "somewhere over the rainbow";
        // init also the parent parameters, like age and phone
    }
    // the rest of the class....
}

In this example, I have the genericguy class; which has 3 parameters that get set when the object is created in the constructor. I would like in the children class called "specificguy, these parameters initialized, as it happens in the parent. How do I do this correctly? In Python you always call the constructor of the parent ("__init__"), but I am not sure about C#

Upvotes: 1

Views: 263

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

There are two parts to the answer:

  • You invoke the base constructor by using : base(...) syntax
  • You do not duplicate assignments made by the base class in the derived class.

In case of your specificguy it means that the constructor should look like this:

public specificguy(string name) : base(name)
{
    // The line where you did "this._name = name;" need to be removed,
    // because "base(name)" does it for you now.
    this._job = "unemployed";
    this._address = "somewhere over the rainbow";
    // init also the parent parameters, like age and phone
}

In Python you always call the constructor of the parent ("__init__")

C# will automatically invoke a no-argument constructor for you; in cases when such constructor is missing, you must provide an explicit invocation through : base(...) syntax.

Upvotes: 1

Andrey Korneyev
Andrey Korneyev

Reputation: 26856

You can declare child class constructor as

public specificguy(string name) : base(name)

Upvotes: 0

Carbine
Carbine

Reputation: 7903

children class:

public class specificguy:genericguy
{
    private string _job;
    private string _address;
    //call the base class constructor by using : base()
    public specificguy(string name):base(name) //initializes name,age,phone
    {
        //need not initialize name as it will be initialized in parent
        //this._name = name;
        this._job = "unemployed";
        this._address = "somewhere over the rainbow";
    }
}

Upvotes: 2

Related Questions