Abstract class and Method


Abstract Class: An abstract class is a special type of class that cannot be instantiated. An abstract class is designed to be inherited by subclass.

you can create abstract class by using abstract keyword.

Note: You cannot create instance of abstract class

Example:

public abstract class Base_Employee
{
     public string First_Name{get;set;}
     public string Last_Name{get;set;}

    public string GetFullName(){
        return this.First_Name +" " + this.Last_Name
    }
    public abstract int GetSalary(); //Once you define Abstract Method, it must override in child class.
}


public class Employee:Base_Employee
{
      public int override GetSalary()
     {
          return 12000;
     }
}


Now, if you try to create instance of Base_Employee then it will not allowed you to do that. usually people used abstract class to define common properties of class which can be used with other classes also.

Abstract Method:An abstract method is a method without body.The Implementation of method done by a derived class(see above ex:).when the derived class inherits the abstract method from abstract class,it must override the abstract method.This requirement enforced at compile time ans it's also called dynamic polymorphism.

Comments