Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access subclass' property from base class' constructor

I discovered something strange, which I didn't expect to work, actually works. I can access a subclass' (constant) property from the base class' constructor:

public abstract class Parent {

  public Parent() {
    var constName = ConstName;            // <-- surprisingly, this works  
    var randomName = RandomName;          // <-- surprisingly, this works  
  }

  public abstract string ConstName { get; }

  public abstract string RandomName { get; }

}


public class Child : Parent {

  public override string ConstName { get { return "Mike"; } }

  public override string RandomName { get { return new Random().Next().ToString(); } }

}

Name is a non-static property rather than field. I always thought a type's initializers (for static and const fields) were executed, then those of its base class, then the base ctor and then the subclass' ctor. Which means that the child is not fully constructed yet while in the parent ctor.

Is this "legal" C# which will work under all circumstances? Why does this work?

EDIT:

No it's not a dupe question. That one doesn't have the class schema in my question.

like image 444
h bob Avatar asked Sep 29 '15 06:09

h bob


People also ask

Can superclass access subclass fields?

Does a superclass have access to the members of a subclass? Does a subclass have access to the members of a superclass? No, a superclass has no knowledge of its subclasses.

Can base class access derived class properties?

// As base-class pointer cannot access the derived class variable.

How do I access parent class fields?

Accessing Parent Class Functions This is really simple, you just have to call the constructor of parent class inside the constructor of child class and then the object of a child class can access the methods and attributes of the parent class. # how parent constructors are called.

Do subclasses inherit fields?

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.


1 Answers

Parent has an abstract property Parent.Name. Because of the word abstract, you promise that instances of (subclasses of) Parent will implement property Name. if they don't, you can't create an object of it.

Note that I say: instances cannot be created. If a subclass doesn't implement property Name, the class can exist, but you can't instantiate it.

Class Parent doesn't implement property Name, hence you can't instantiate Parent.

Class Child however implements Name, hence you can instantiate it, and because you promised that every object (=instantiation) of class Parent has a property Name, you can be sure that although all you know is that it is a Parent, you also know that it has a Name.

This is the basic principle of polymorphism in subtyping Wikipedia about polymorphism

It might be the main reason why you would want to subclass.

like image 99
Harald Coppoolse Avatar answered Sep 22 '22 14:09

Harald Coppoolse