Inheriting Subclasses from Classes
Learn how to create subclasses in C# and understand concepts like hiding, overriding, and using the base keyword for accessing base class members.
We’re diving into the world of creating subclasses that inherit from existing classes. By following a few simple steps, we’ll see how easy it is to create subclasses in C# and inherit all the capabilities of the base class while adding specific features for employees. Let’s get started!
Creating a subclass inherited from a class
The Person
type we created earlier derived (inherited) from the object, the alias for System.Object
. Now, we will create a subclass that inherits from Person
:
Step 1: In the PacktLibrary
project, add a new class file named Employee.cs
.
Step 2: Modify its contents to define a class named Employee
that derives from Person
, as shown in the following code:
namespace Packt.Shared;public class Employee : Person{}
Step 3: In the PeopleApp
project, in Program.cs
, add statements to create an instance of the Employee
class, as shown in the ...