Additional Topics on Inheritance
Explore key inheritance concepts in C# such as access to base class members, protection levels affecting field visibility, and how to properly invoke base constructors using the base keyword. Understand constructor inheritance rules and how to implement constructors in derived classes for robust object-oriented design.
We'll cover the following...
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.
Despite our reasonable assumption, running the ...