Inheriting from classes

The Person type we created earlier implicitly derived (inherited) from System.Object. Now, we will create a new class that inherits from Person.

Add a new class named Employee.cs to the Ch07_PacktLibrary project.

Modify its code as shown in the following code:

    using System; 
 
    namespace Packt.CS7 
    { 
      public class Employee : Person 
      { 
      } 
    } 

Add statements to the Main method to create an instance of the Employee class:

    Employee e1 = new Employee  
    { 
      Name = "John Jones",  
      DateOfBirth = new DateTime(1990, 7, 28)  
    }; 
    e1.WriteToConsole(); 

Run the console application and view the output:

John Jones was born on Saturday, 28 July 1990

Note that the Employee class has inherited all the members of Person.

Extending classes

Now, we will add some employee-specific ...

Get C# 7 and .NET Core: Modern Cross-Platform Development - Second Edition 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.