abstract class inheritance in c#.net

Implementing abstract class in c#.net is similar to that in Visual Basic.net. However, there is a difference in syntax while creating abstract class, abstract members and derived class in c#.

Click here to know How to use abstract class inheritance in Visual Basic.Net.

To declare an abstract class in c#, we need to declare it using the abstract keyword in the class declaration. The following code demonstrates abstract class inheritance in c#.net.


abstract class Employee : 
    {
private string  mFirstName;
private string mLastName;

        public Employee(string fname, string lname)
        {
          mFirstName = fname;
          mLastName = lname;
    
        }
        public Employee()
        { 
        
        }

        public string FName
        {
            get
            {
                return mFirstName;
            }
            set
            {
                mFirstName = value;
            }
        }

        public string LName
        {
            get
            {
               return mLastName;
            }
            set
            {
                mLastName = value;
            }
        }



        public abstract double InComeEarned();
      
    }


Abstract members of an abstract class do not contain any implementation code. Abstract methods are declared using the abstract keyword as shown in above code. These abstract methods are overridden by subclasses and so there is no implementation in the abstract methods.



The syntax for deriving a class from a base class or from an abstract base class is similar. The following code derives a class - 'RegularEmployee' from an abstract class - 'Employee'.

Implementing Abstract Class in C#.

The next step is to implement the abstract method InComeEarned() in the derived class. To implement abstract members in a derived class, the override keyword must be used.

class RegularEmployee : Employee
    {
        private double  mSalary;

    public RegularEmployee(string fname, string lname, double salary)   : base()
    {
        mSalary = salary;
     }

    public double Salary
    {
        get
        {
            return mSalary;
        }
        set
        {
           mSalary = value;
       }
    }

               
        public override  double InComeEarned()
        {
             return Salary;
        }



Authorship
By Bharati Karedla