Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Thumb Rule in OOPS for Inheritance – Part 1

4.20/5 (3 votes)
20 May 2012CPOL1 min read 15.2K   1  
Wherever object of parent class is expected, object of child class can go.

Let's look at the very important rule in OOPS for inheritance.

“Wherever object of parent class is expected, object of child class can go.”

Where This Can Be Used?

This rule is used to design loosely coupled systems so that we can optimize the code, thereby improving the system. Consider the following system:

TightlyCoupledSystem

Tightly Coupled System

The above system has a common function PrintCheque() which is tightly coupled with specific type. PrintCheque() does the same thing for all three classes but still it is defined in each class and takes specific type as parameter. This makes the system tightly coupled to the specific type.

Problem

Although the function does the same thing, it is defined in all three classes and takes parameter of specific type making it bound to that type. Also, in future, if we add another type, we have to redefine this function which will take object of that new type as parameter.

Solution

We declare a new class called Employee and inherit these classes from employee class.

LooselyCoupledSystem

Loosely Coupled System

The above designed system is loosely coupled. You can see that the PrintCheque() function now takes parameter of type Employee, so any class that inherits from Employee can be passed as parameter. Also, any new type added, when inherited from Employee class, will be able to call this function thus making our system a loosely coupled system.

Source Code

Employee Class:

C#
abstract class Employee
{
   private string _name;
   private double _salary;

   public Employee(string mname, double mamt)
   {
       _name = mname;
       _salary = mamt;
   }

   public string Name {
       get
       {
           return _name;
       }
   }

   public double Salary
   {
       get {
           return _salary;
       }
   }

   public abstract double NetSalary();

   public static void PrintCheque(Employee temp)
   {
       Console.WriteLine("Name={0} Salary={1}", temp.Name, temp.NetSalary());
   }
}

Manager Class:

C#
class Manager:Employee
{
    private double _taxes = 0.25;

    public Manager(string mname, double mamt)
        : base(mname, mamt)
    { }

    public override double NetSalary()
    {
        return Salary * (1 - _taxes);
    }

    public void PlanProjects()
    {
        Console.WriteLine("Manager plans projects");
    }
}

Analyst Class:

C#
class Analyst:Employee
{
    double _taxes = 0.20;
    double _compoff = 0.10;

    public Analyst(string mname, double mamt)
        : base(mname, mamt)
    {
    }

    public override double NetSalary()
    {
        return Salary * (1 - _taxes + _compoff);
    }

    public void DesignSystems()
    {
        Console.WriteLine("Analyst designs systems");
    }
}

SalesMan Class:

C#
class SalesMan:Employee
{
    double _comm = 0.05;

    public SalesMan(string mname, double mamt) : base(mname, mamt) { }

    public override double NetSalary()
    {
        return Salary * (1 + _comm);
    }

    public void Markets()
    {
        Console.WriteLine("Salesman sells");
    }
}

Main

C#
class Program
{
    static void Main(string[] args)
    {
        Manager emp1 = new Manager("abc", 700000);
        Analyst emp2 = new Analyst("pqr", 500000);
        SalesMan emp3 = new SalesMan("lmn", 200000);

        Employee.PrintCheque(emp1);
        Employee.PrintCheque(emp2);
        Employee.PrintCheque(emp3);
    }

    //inheritance is not always used for relationships
    //in this case inheritance is used to exploit law of polymorphism
    //by making the system loosely bound so that we dont have to
    //write PrintCheque() function again and again for each type of employee

    //law of polymorphism says
    //whereever object of parent class is expected object of child class can go
    //so we designed the printcheque function such that it can be used for any
    //child class of employee class making it loosely coupled system.
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)