Search⌘ K
AI Features

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.

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.

C#
namespace Inheritance
{
public class EducationalOrganization
{
// Mark this field as protected for child classes to access it
private string _name;
public string Name
{
get { return _name; }
}
}
}

Despite our reasonable assumption, running the ...