Inheriting from classes
The Person type we created earlier implicitly derived (inherited) from System.Object. Now, we will create a class that inherits from Person:
- Add a new class named
Employeeto thePacktLibraryproject. - Modify its statements, as shown in the following code:
using System; namespace Packt.Shared { public class Employee : Person { } } - Add statements to the
Mainmethod to create an instance of theEmployeeclass, as shown in the following code:Employee john = new Employee { Name = "John Jones", DateOfBirth = new DateTime(1990, 7, 28) }; john.WriteToConsole(); - Run the console application and view the result, as shown in the following output:
John Jones was born on a Saturday
Note that the Employee class has inherited all the members of Person.
Extending classes to add functionality
Now, we will add some employee-specific members to extend the...