Are private members inherited in C #?

I just saw one textbook that said:

Class Dog { private string Name; } Class SuperDog:Dog { private string Mood; } 

Then it was displayed by UML that SuperDog also inherits the name. I tried, but it seems to me that only public members are inherited. At least I could not access the name if it was not declared as public.

+51
inheritance private c #
Jun. 01 '10 at 2:55 a.m.
source share
14 answers

The derived class has access to the public, protected, internal, and protected internal elements of the base class. Even if a derived class inherits private members of the class base, it cannot access these members. However, all these private members are still present in the derived class and can do the same work as in the base class. For example, suppose a protected base class method accesses a private field. That the field must be present in the derived class for the inherited base class for the correct operation.

From: http://msdn.microsoft.com/en-us/library/ms173149.aspx

So technically, yes, but practically not.

+92
Jun 01 '10 at 2:58 p.m.
source share

Everything from the base class is inherited from the derived class. . marked as private, not accessible to derived classes for integrity purposes, if you need to make them available in a derived class, mark members as protected.

There are various levels of accessibility for participants in the context of inheritance.

public : all public members of the base class are available in the derived class and instances of the derived class.

protected : all protected members of the base class are available in the derived class, not in instances of the derived class.

protected internal : all protected internal members of the base class are available in the derived class and instances of the derived class created within the same assembly.

internal : all internal members of the base class are available in the derived class and instances of the derived class within the same assembly.

private : no private members of the base class are available in the derived class and instances of the derived class.

+38
Jun 01 '10 at 15:07
source share

SuperDog inherits the Name field, yes.

SuperDog will NOT have access to the field, so there is no practical use (as far as SuperDog is concerned).

+16
Jun 01 '10 at 2:59 p.m.
source share

Yes, although heirs cannot access this member.

If you can access it with this, declare it as protected .

+1
Jun 01 2018-10-06
source share

No, it is not.

The protected modifier can provide fields to derived classes, but this is usually considered a bad idea in terms of maintenance. Instead, you should use protected properties.

0
Jun 01 2018-10-01
source share

Private members are not available to descendants of the class.

I’m not sure about all access modifiers, but in the most basic, only public and protected members are available.

0
Jun 01 2018-10-01
source share

An element in the base class must be at least protected so that it can be seen in the derived class.

0
Jun 01 2018-10-01
source share

try the protected keyword, not public / private:

http://msdn.microsoft.com/en-us/library/bcd5672a(VS.71).aspx

0
Jun 01 2018-10-01
source share

Instead, make the name protected or public , this will be available. Private members not accessible from derived classes

0
Jun 01 '10 at 2:58 p.m.
source share

According to others, private members are inherited. Membership is another subject, but not completely unrelated to inheritance. It is important to understand that all members are inherited regardless of their access modifier, since it affects the size of subclasses. Consider the following code.

 public class Foo { private int a; public int b; } public class Bar : Foo { private int c; public int d; } 

Foo will consume 16 bytes on the heap. 4 for synchronization, 4 for type information (method table) and 4 for int variables for a total of 12. Bar , on the other hand, will consume 24 bytes. 4 for synchronization, 4 for type information (method table), 4 for int fields inherited from Foo , and 4 for int fields in Bar for a total of 24.

0
Jun 01 '10 at 15:43
source share

Yes, they are inherited. But you cannot access them because they are private :).

0
Jun 2 '10 at 9:22
source share

Yes, but they are not available, therefore, looking at it, you can honestly say that they are not inherited. But yes, they really

0
Aug 24 '10 at 11:50
source share

Private members can be seen inside a derived class:

 public class Person { private string message; public override string ToString() { return message; } public static Person CreateEmployee() { return new Employee(); } class Employee : Person { public Employee() { this.message = "I inherit private members!"; } } } 

Credit for example refers to KodefuGuru in this thread under MSDN .

0
Mar 13 '14 at 15:00
source share

People said this, but here is an example of why you need private fields in a derived class:

 class Program { static void Main(string[] args) { var r = new Random(); foreach(var i in Enumerable.Range(0,100)) new Derived(r).Printer(); Console.Read(); } } public class Base { private Random r; public Base(Random r) { this.r = r; } protected void Print() { Console.WriteLine(r.Next(1, 10000)); } } public class Derived : Base { public Derived(Random r) : base(r) { } public void Printer() { base.Print(); } } 
0
Mar 27 '15 at 14:23
source share



All Articles