Inheriting from classes
The Person
type we created earlier is 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 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 members to extend the class.
In the Employee
class, add the following code to define two properties:
public string EmployeeCode { get; set; } public DateTime HireDate { get; set; }
Back in the Main
method, add...