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 to import the Packt.CS6
namespace and add statements to the Main
method to create an instance of the Employee
class:
using Packt.CS6; using System; using static System.Console; namespace Ch07_InheritanceApp { class Program { ...