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
Employee
to thePacktLibrary
project. - Modify its statements, as shown in the following code:
using System; namespace Packt.Shared { public class Employee : Person { } }
- Add statements to the
Main
method to create an instance of theEmployee
class, 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
Now, we will add some employee-specific members to extend the class.
- In the
Employee
class, add the following code to define two properties...