Inheriting from classes

Inheritance is a mechanism for code reuse where a derived (or sub) class is based on a base (or super) class thereby having access to all of the base class' members. The Person type we created earlier implicitly derived (inherited) from System.Object. Now, we will create a new class that explicitly inherits from Person.

In Solution Explorer, ensure that the Ch06_PacktLibrary project has the focus. Next, from the Project menu, choose Add Class… or press Shift + Alt + C. Name the class Employee.

Modify its code as follows:

using System;
namespace Packt.CS6
    public class Employee : Person

Add a new console application named Ch07_InheritanceApp.

Add a reference to the Ch06_PacktLibrary assembly.

Modify the Program.cs file ...

Get C# 6 and .NET Core 1.0: Modern Cross-Platform Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.