Class Inheritance
In this chapter you will learn:
Inheritance
Classes (but not structs) support the concept of inheritance. A class that derives from the base class automatically has all the public, protected, and internal members of the base class except its constructors and destructors.
In C# one class can only inherit from a single class. Private members are not inherited.
A class that is inherited is called a base class. The inheriting class is called a derived class. A derived class is a specialized version of a base class. A derived class has all of the variables, methods, properties, operators, and indexers from the base class. A derived class adds its own unique elements.
The general form of a class declaration that inherits a base class:
class derived-class-name : base-class-name {
// body of class
}
The following code defines a Person
class.
Person
class has all common fields for all
person
types.
class Person{
public string name;
}
When declaring the Employee
class we
can build the employee type based on person type.
class Employee:Person{
public string companyName;
}
Employee
inherits the name field from Person
.
Next chapter...
What you will learn in the next chapter: