Inheritance

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Inheritance

 What is Inheritance?

 Inheritance is a feature in object-oriented programming that allows a class (child or


derived class) to inherit methods, properties, and other characteristics from another
class (parent or base class).
 This promotes code reuse and establishes a hierarchical relationship between classes.

 How is it used?

 A parent class defines common functionality.


 A child class extends the parent class and can override or add new functionality.

Design a system for employees in a company. The system should have a base class Employee
with common attributes like name and salary. There are specific types of employees:

1. Full-time employees, who have an annual bonus.


2. Part-time employees, who are paid hourly.

Coding

using System;

// Base class Employee


class Employee
{
public string Name { get; set; }
public double BaseSalary { get; set; }

public Employee(string name, double baseSalary)


{
Name = name;
BaseSalary = baseSalary;
}

// Method to display employee details


public virtual void DisplayDetails()
{
Console.WriteLine("Employee Name: " + Name);
Console.WriteLine("Base Salary: $" + BaseSalary);
}

// Virtual method to calculate total pay


public virtual double CalculateTotalPay()
{
return BaseSalary;
}
}

// Derived class for Full-Time Employees


class FullTimeEmployee : Employee
{
public double AnnualBonus { get; set; }

public FullTimeEmployee(string name, double baseSalary, double annualBonus)


: base(name, baseSalary)
{
AnnualBonus = annualBonus;
}

// Overriding CalculateTotalPay method


public override double CalculateTotalPay()
{
return BaseSalary + AnnualBonus;
}

// Adding additional details specific to Full-Time Employees


public override void DisplayDetails()
{
base.DisplayDetails();
Console.WriteLine("Annual Bonus: $" + AnnualBonus);
Console.WriteLine("Total Pay: $" + CalculateTotalPay());
}
}

// Derived class for Part-Time Employees


class PartTimeEmployee : Employee
{
public int HoursWorked { get; set; }
public double HourlyRate { get; set; }

public PartTimeEmployee(string name, double baseSalary, int hoursWorked, double


hourlyRate)
: base(name, baseSalary)
{
HoursWorked = hoursWorked;
HourlyRate = hourlyRate;
}

// Overriding CalculateTotalPay method


public override double CalculateTotalPay()
{
return BaseSalary + (HoursWorked * HourlyRate);
}

// Adding additional details specific to Part-Time Employees


public override void DisplayDetails()
{
base.DisplayDetails();
Console.WriteLine("Hours Worked: " + HoursWorked);
Console.WriteLine("Hourly Rate: $" + HourlyRate);
Console.WriteLine("Total Pay: $" + CalculateTotalPay());
}
}

// Main program to demonstrate inheritance


class Program
{
static void Main()
{
// Create a Full-Time Employee
FullTimeEmployee fullTimeEmp = new FullTimeEmployee("Alice", 50000, 10000);
Console.WriteLine("Full-Time Employee Details:");
fullTimeEmp.DisplayDetails();

Console.WriteLine();
// Create a Part-Time Employee
PartTimeEmployee partTimeEmp = new PartTimeEmployee("Bob", 1000, 20, 25);
Console.WriteLine("Part-Time Employee Details:");
partTimeEmp.DisplayDetails();
}
}

Output

Full-Time Employee Details:

Employee Name: Alice

Base Salary: $50000

Annual Bonus: $10000

Total Pay: $60000

Part-Time Employee Details:

Employee Name: Bob

Base Salary: $1000

Hours Worked: 20

Hourly Rate: $25

Total Pay: $1500

Virtual keyword

The virtual keyword in C# allows a method in a base class to be overridden in derived


classes. This means the derived class can provide its own implementation of the method,
customizing its behavior while still maintaining the same method signature.

You might also like