C
C
CodeOfYourLIfe2019-03-30 03:20:13
C++ / C#
CodeOfYourLIfe, 2019-03-30 03:20:13

Inheritance of private fields?

I've been studying C# for a while and settled on the "inheritance" chapter, perhaps later this issue is considered in more detail, but I would like to receive an answer now

class B
   {
      private int b;
      public int GetSetb { get { return b; } set { b = value; } }
   }
   class C : B
   {       
   }

The variable b is defined as private and, as far as I understand, does not belong to class C, but when I assign a value to it with the GetSetb property, this code works and I can display b although it (as far as I understand) does not belong to class C

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2019-03-30
@byte916

The child class does not know anything about the parent's private fields, and may contain fields/methods/properties of the same name, and not necessarily of the same type and with the same access modifier.
When you create a variable with the type of a child class, then all parent constructors work out for you, and all fields of parent classes are created in memory. They are required for all inherited methods and properties to work, but private fields cannot be directly accessed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question