Sobes.tech
Junior — Middle

Which components or properties of the base class are not passed to the classes that inherit from it?

sobes.tech AI

Answer from AI

In C#, when inheriting, the base class passes all its non-static members to its derived classes, except those declared with the access modifier private. That is, components or properties with the private modifier are not directly accessible in derived classes.

Also, static members of a class are not inherited in the usual sense; they belong to the class itself, not to instances, so they are accessed through the name of the base class.

Example:

class BaseClass
{
    private int privateField = 1; // not accessible in derived class
    protected int protectedField = 2; // accessible in derived class
    public int publicField = 3; // accessible in derived class
    public static int staticField = 4; // not inherited, accessed via BaseClass.staticField
}

class DerivedClass : BaseClass
{
    public void ShowFields()
    {
        // Console.WriteLine(privateField); // compilation error
        Console.WriteLine(protectedField); // OK
        Console.WriteLine(publicField); // OK
        Console.WriteLine(staticField); // error, need BaseClass.staticField
        Console.WriteLine(BaseClass.staticField); // OK
    }
}

Thus, private members and static members are not passed to derived classes as instance members.