using System; class Program { static void Main(string[] args) { Employee employee1 = new Employee("John", "Smith", 50000); Console.WriteLine(employee1.GetInfo()); Manager manager1 = new Manager("Jane", "Doe", 75000, "Sales"); Console.WriteLine(manager1.GetInfo()); } } class Employee { protected string firstName; protected string lastName; protected double salary; public Employee(string firstName, string lastName, double salary) { this.firstName = firstName; this.lastName = lastName; this.salary = salary; } public virtual string GetInfo() { return "Name: " + firstName + " " + lastName + ", Salary: $" + salary; } } class Manager : Employee { private string department; public Manager(string firstName, string lastName, double salary, string department) : base(firstName, lastName, salary) { this.department = department; } public override string GetInfo() { return base.GetInfo() + ", Department: " + department; } }In this example, we have a base class
Employee
and a derived classManager
. TheEmployee
class has protected fieldsfirstName
,lastName
, andsalary
, and a methodGetInfo()
that returns a string with the employee’s name and salary. TheManager
class inherits fromEmployee
and adds a private fielddepartment
, as well as an overriddenGetInfo()
method that includes the department in the output string.In the
Main()
method, we create anEmployee
object and aManager
object, and call their respectiveGetInfo()
methods. BecauseManager
inherits fromEmployee
, we can treat theManager
object as anEmployee
object and call theGetInfo()
method on it, which is overridden in theManager
class to include the department.When we run the program, it outputs the following:
Name: John Smith, Salary: $50000
Name: Jane Doe, Salary: $75000, Department: SalesThis demonstrates inheritance, where a derived class inherits the fields and methods of a base class and can add its own fields and methods or override existing ones. This promotes code reuse and allows for more efficient and modular design.