Implementing Inheritance in C#.NET


Situation Where to implement inheritance
  • Assume that a company has N-no of branches and   asked to computerize Branches Details of the  company, then we create a class like…



  • Later after, some period of time, company  also asked to computerize Employees  details of each Branch
 

  • If we create two classes individually  without inheritance we need to create objects for every class individually and  objects will have maintain separate references to the members of the classes like…



  • Where Obj1 is object to ClsBranch class and Obj5 is object to ClsEmployee class
  • So it becomes difficult to identify which employee belongs to  which branch and integrate the ClsBranch class objects with ClsEmployee class objects
  • So if we derive the ClsEmployee class from ClsBranch class we create object to the Derived class ClsEmployee then it will represent both the classes and will maintain reference to the members of both base and derived classes like…

 

  • Where Obj1 is object to the derived class ClsEmployee
  • As ClsEmployee is inherited from ClsBranch class, ClsEmployee class will get all the features of  ClsBranch class

Example :-

Class Diagram :-

 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CASInheritance
{
    class ClsBranch
    {
        int BCode;
        string BName;
        string BAddress;
        public void GetBData()
        {
            Console.WriteLine("Enter Branch Details:-  ");
            BCode = Convert.ToInt32(Console.ReadLine());
            BName = Console.ReadLine();
            BAddress = Console.ReadLine();
        }
        public void DisplayBData()
        {
            Console.WriteLine("Branch  Details are:-");
            Console.WriteLine("Branch Code is       :-          "+BCode);
            Console.WriteLine("Branch Name is       :-          "+BName);
            Console.WriteLine("Branch Address is    :-          "+BAddress);
        }
    }
    class ClsEmployee:ClsBranch
    {
        int EmpId;
        string EName;
        int EAge;
        string EAddress;
        public void GetEmpData()
        {
            Console.WriteLine("Enter Employee Details:- ");
            EmpId = Convert.ToInt32(Console.ReadLine());
            EName = Console.ReadLine();
            EAge = Convert.ToInt32(Console.ReadLine());
            EAddress = Console.ReadLine();

        }
        public void DisplayEmpData()
        {
            Console.WriteLine("Employee Details Are :-");
            Console.WriteLine("Employee Id is       :-         "+EmpId);
            Console.WriteLine("Employee Name is     :-         "+EName);
            Console.WriteLine("Employee Age is      :-         "+EAge);
            Console.WriteLine("Employee Address is  :-         "+EAddress);
        }
    }
    class ClsSInheritance
    {
        static void Main()
        {
            ClsEmployee obj1 = new ClsEmployee();
            obj1.GetEmpData();
            obj1.GetBData();
            obj1.DisplayEmpData();
            obj1.DisplayBData();
            Console.ReadLine();
        }
       
    }
}



Output :-


  • In the above example we made the functions GetEmpData( ) and DisplayEmpData( ) of ClsBranch class as public, because to access from outside the class i.e. from ClsSInheritance
  • And the Data Fields BCode, Bname and BAddress are private by default, so they are accessible within the same class only
  • But if we do not want to give accessibility of the class members to the non derived class (ClsSInheritance) and would like to give to the Derived class (ClsEmployee) then we use protected to the members
  • And our code will be like…

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CASInheritance
{
    class ClsBranch
    {
        int BCode;
        string BName;
        string BAddress;
        protected void GetBData()
        {
            Console.WriteLine("Enter Branch Details:-  ");
            BCode = Convert.ToInt32(Console.ReadLine());
            BName = Console.ReadLine();
            BAddress = Console.ReadLine();
        }
        protected void DisplayBData()
        {
            Console.WriteLine("Branch  Details are:-");
            Console.WriteLine("Branch Code is       :-          "+BCode);
            Console.WriteLine("Branch Name is       :-          "+BName);
            Console.WriteLine("Branch Address is    :-          "+BAddress);
        }
    }
    class ClsEmployee:ClsBranch
    {
        int EmpId;
        string EName;
        int EAge;
        string EAddress;
        public void GetEmpData()
        {
            Console.WriteLine("Enter Employee Details:- ");
            EmpId = Convert.ToInt32(Console.ReadLine());
            EName = Console.ReadLine();
            EAge = Convert.ToInt32(Console.ReadLine());
            EAddress = Console.ReadLine();
            base.GetBData();/*Here base is the keyword used to call the
            Superclass method from subclass */

        }
        public void DisplayEmpData()
        {
            Console.WriteLine("Employee Details Are :-");
            Console.WriteLine("Employee Id is       :-         "+EmpId);
            Console.WriteLine("Employee Name is     :-         "+EName);
            Console.WriteLine("Employee Age is      :-         "+EAge);
            Console.WriteLine("Employee Address is  :-         "+EAddress);
            base.DisplayBData();/*Here base is the keyword used to call the
            Superclass method from subclass*/
        }
    }
    class ClsSInheritance
    {
        static void Main()
        {
            ClsEmployee obj1 = new ClsEmployee();
            obj1.GetEmpData();
            obj1.DisplayEmpData();
            Console.ReadLine();
        }
       
    }
}



Output :-



No comments:

Post a Comment