Holi Sale. Get upto 40% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Browse Tutorials
C# Class Abstraction

C# Class Abstraction

12 Mar 2024
Advanced
187K Views
6 min read
Learn via Video Course & by Doing Hands-on Labs

C# Programming For Beginners Free Course

Abstract Class in C#: An Overview

Data abstraction is the process of hiding certain information and showing only essential information to the user which is what the user wants. In this C# Tutorial, we will explore abstract classes which will include, What is an abstract class, c# abstract class with examples, when to use an abstract class, and features of abstract class. So let's see first What is an abstract class in C#.

What is an abstract class?

  • Abstraction can be achieved with either abstract classes or interfaces
  • An abstract class is a special type of class that cannot be instantiated and acts as a base class for other classes.
  • Abstract class members marked as abstract must be implemented by derived classes.
  • An abstract class can be used as a base class and all other derived classes should be implemented the abstract class definitions.
  • All the abstract methods should be implemented in all non-abstract classes using the keyword called an override.
  • After overriding, the abstract method is in the non-abstract class.
  • We can derive this class in another class and again we can override the same abstract method with it ultimately.
  • The purpose of an abstract class is to provide basic or default functionality as well as common functionality that multiple derived classes can share and override.

Syntax of Abstract Class:

Let's see the syntax of an abstract class in C# that has one abstract method declared which is "drive()". Its implementation can be provided by derived classes as required

 using System; 
public abstract class Car 
{ 
 public abstract void drive(); 
} 

Example:

A class library may define an abstract class that is used as a parameter to many of its functions and require programmers to use that library to provide their own implementation of the class by creating a derived class. In C#, System.IO.FileStream is an implementation of the System.IO.Stream abstract class. Let's elaborate on this in C# Compiler.

   using System;
namespace MyApplication
{
  // Abstract class
  abstract class Company
  {
    // Abstract method (does not have a body)
    public abstract void CompanyName();
    // Regular method
    public void CompanyRevenue()
    {
      Console.WriteLine("  The Company Revenue is 3 Crore.");
    }
  }
  
  // Derived class (inherit from Company)
  class Employee : Company
  {
    public override void CompanyName()
    {

      Console.WriteLine("The Company Name is: DotNetTricks Innovation .");
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      Employee emp = new Employee(); 
      emp.CompanyName();
      emp.CompanyRevenue();
    }
  }
}    

Output

 The Company Name is: DotNetTricks Innovation .The Company Revenue is 3 Crore. 

Explanation

We have taken an abstract class called "Company" and an abstract method called "ComapanyName". We also took one regular method called" CompanyRevenue".We override an abstract method in the Derived class employee. And fetch properties of the base class by creating an object of the derived class this is what abstraction worked

Read More - C# Programming Interview Questions

What is Abstract methods?

  • A method that is declared as an abstract method, has no “body” and it can be declared inside the abstract class only which is the thumb rule.
  • An abstract method should be implemented in all non-abstract classes using the keyword called "override".
  • After overriding the methods, the abstract method is in the non-abstract class context.

Syntax of abstract method:

 public abstract void getData(); // where the method called 'getData()' is a abstract method    

Features of Abstract Class

  1. An abstract class cannot be instantiated.

  2. An abstract class contains abstract members as well as non-abstract members.

  3. An abstract class cannot be a sealed class because the sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.

  4. A non-abstract class that is derived from an abstract class must include actual implementations of all the abstract members of the parent abstract class.

  5. An abstract class can be inherited from a class and one or more interfaces.

  6. An Abstract class can have access modifiers like private, protected, and internal with class members. But abstract members cannot have a private access modifier.

  7. An Abstract class can have instance variables (like constants and fields).

  8. An abstract class can have constructors and destructors.

  9. An abstract method is implicitly a virtual method.

  10. Abstract properties behave like abstract methods.

  11. An abstract class cannot be inherited by structures.

  12. An abstract class cannot support multiple inheritances.

Common design guidelines for Abstract Class

  1. Don't define public constructors within an abstract class. Since an abstract class cannot be instantiated and constructors with public access modifiers provide visibility to the classes that can be instantiated.

  2. Define a protected or an internal constructor within an abstract class. Since a protected constructor allows the base class to do its own initialization when sub-classes are created and an internal constructor can be used to limit concrete implementations of the abstract class to the assembly that contains that class.

When to use an abstract class

  1. Need to create multiple versions of your component since versioning is not a problem with abstract classes. You can add properties or methods to an abstract class without breaking the code and all inheriting classes are automatically updated with the change.

  2. Need to provide default behaviors as well as common behaviors that multiple derived classes can share and override.

Conclusion:

The concepts of abstraction with the methods and classes are an extension to the inheritance inheritance we have been discussing that with the help of a parent class, we can provide property to the child class that can be consumed by the child classes which gives us the code re-usability while sticking to the security accessibility. I hope, now you have got everything about the abstract class. Also, consider our C# Programming Course. I would like to have feedback from my blog readers. Your valuable feedback, questions, or comments about this article are always welcome.

FAQs

Q1. What is use of abstract class in C#?

to provide a blueprint for derived classes and set some rules what the derived classes must implement when they inherit an abstract class.

Q2. What is the disadvantage of abstract class in C#?

 Limited Inheritance

Q3. Why abstract class is used?

To provide a base for subclasses to extend and implement the abstract methods and override or use the implemented methods in abstract class

Q4. Why abstract class is required?

To provide a blueprint or a skeletal framework for derived classes

Q5. What is difference between abstract class and interface C#?

Abstract classes have static members. The interface does not have static members.

Take our free csharp skill challenge to evaluate your skill

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this