Additional Topics on Inheritance
Discover more details of working with inheritance in .NET
We'll cover the following...
Access the base class members
Inheritance is great, but what exactly do we inherit? All members of the base class or only some of them? We can answer these questions by trying to access base class members. Let’s consider an example:
public class EducationalOrganization
{
private string _name;
public string Name
{
get { return _name; }
}
}
public class School : EducationalOrganization
{
}
The School
class inherits from EducationalOrganization
. It’s reasonable to assume that School
inherits all members of EducationalOrganization
, including the _name
field.
Press + to interact
Program.cs
EducationalOrganization.cs
School.cs
namespace Inheritance{public class EducationalOrganization{// Mark this field as protected for child classes to access itprivate string _name;public string Name{get { return _name; }}}}
Despite our reasonable assumption, running the ...