Explain Abstraction, Abstract Class & Abstract Members

Explain Abstraction, Abstract Class & Abstract Members

Abstraction, Abstract Class & Abstract Members

Abstraction is similar to data encapsulation in OOP. It means showing only the essential information and hiding the other irrelevant information from the user
Abstraction can be achieved with either abstract classes or interfaces

Abstract Modifier
The Abstract modifier can be used with a class, method, property, indexer or event
The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class.
Or in other words, members marked as abastract or included in an abstract class, must be implemented by the class that derivers from the abstract class.

Abstract Class:
is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).

The characteristics of an abstract class :-
1. Abstract classes exist primarily for inheritance, You can't create an instance of an abstract class
2. An abstract class can have both abstract and regular methods
3. An abstract class must have at least one abstract method.
4. It is always public.
5. An Abstract class cannot support multiple inheritance

Purpose of an abstract class :-
The purpose of an abstract class is to provide a common definition of the base class that multiple derived classes can share.

public abstract class BaseEmployee
    public string EmployeeID { get; set; }
    public string EmployeeName { get; set; }
    public string EmployeeAddress { get; set; }

    public abstract double CalculateSalary(int hoursWorked);

public class FullTimeEmployee : BaseEmployee
    public override double CalculateSalary(int hoursWorked)
        return hoursWorked * 60.00+3700;

public class ContractEmployee : BaseEmployee
    public override double CalculateSalary(int hoursWorked)
        return hoursWorked * 65.00;

class Program
    static void Main(string[] args)
        //BaseEmployee Object is created with no compilation error
        BaseEmployee baseEmployee = new BaseEmployee();
        baseEmployee.EmployeeID = "EMP-001";
        baseEmployee.EmployeeAddress = "New Delhi";
        baseEmployee.EmployeeName = "Deepak Talwar";

        BaseEmployee fullTimeEmployee = new FullTimeEmployee();
        var fteSalary = fullTimeEmployee.CalculateSalary(40);

        //Base Employee Calculate Salary can be called that shouldn't be accessible
        var baseSalary = baseEmployee.CalculateSalary(34);

Abstract class can HAVE constructor even though it cannot be initiated, this constructor can be used to initiate common properties, e.g., GUID for each derived class.
Abstract class constructor is automatically called by derived class constructor (Parent class constructor is called first followed by child class constructor)
Since Abstract class constructor can only be called by derived class, constructor access modifier as Public does not make sense, it should be Protected.
Abstract function can be called from Abstract class constructor, whenever instance of derived class is created, overridden abstract method in derived class would be called.

Abstract Members:
1. An abstract member has a signature, but no function body and must be overridden in any non-abstract derived class.
2. Static members can't be abstract.
3. An Abstract method is implicity a virtual method.

What situations will you prefer abstract classes and not interfaces?
1. An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it.
2. When You want to share code among several closely related classes
3. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes.
4. An abstract class is also good if we want to declare non-public members. In an interface, all methods must be public.
5. If we want to add new methods in the future, then an abstract class is a better choice. Because if we add new methods to an interface, then all of the classes that already implemented that interface will have to be changed to implement the new methods.
6. Abstract classes provide a simple and easy way to version our components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, we must create a whole new interface.
7. Abstract classes have the advantage of allowing better forward compatibility. Once clients use an interface, we cannot change it; if they use an abstract class, we can still add behavior without breaking the existing code.

Rule for Virtual/ Abstract / Override
1. When using the Virtual or Override Keyword, you cannot declare it as a private method.
2. Both Virtual and Override method signatures must be identical, i.e. they must have same name, number and type of parameters. Both methods must return the same type.
3. Your can override only a virtual method, marked with virtual keyword.
4. If a virtual method is declared abstract, it is still virtual to any class inheriting from the abstract class. A class inheriting an abstract method cannot access the original implementation of the method

Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.