Jan Bohac
Jan Bohac

Reputation: 69

What is instantiated when calling base constructor

Suppose I have this code

class administrator: uzivatel
{
    public administrator(string jmeno,string prijmeni) : base(jmeno,prijmeni) { }

    public string Jmeno
    {
        get { return base.jmeno; }
    }
}

Class administrator inherits from class uzivatel and calls base constructor which instantiates the variable jmeno and variable prijmeni.

Those variables are inherited from parent class. The property jmeno will return this.jmeno, which I understand, but it can also can return the value of base.jmeno.

How is possible to return base.jmeno when there is no created object of class uzivatel?

Upvotes: 0

Views: 66

Answers (1)

JNYRanger
JNYRanger

Reputation: 7097

You may want to learn a bit more about how inheritance and how it works.

The reason why this works is because an administrator object IS A uzivatel object! This means when you instantiate adminstrator with a constructor that calls upon the base class, then the constructor for the base class is also called. This allows the other variables to be initialized.

You also mention

The property jmeno will return this.jmeno, which I understand, but it can also can return the value of base.jmeno.

Not exactly. In this situation base.jmeno == this.jmeno because as stated earlier your administrator object IS A uzivatel object. Remember inheritance means the subclass inherits all of the properties, fields, and methods of its superclass.

Upvotes: 2

Related Questions