What is Abstract Class in C#? | Definition, Properties vs Examples

Abstraction in programming refers to concealing unnecessary, albeit highly important, information and making only certain key or requisite information visible to the user. This is made possible through the use of abstract classes.

Abstract class in C# is essentially a class that does not permit object creation. ‘Abstract’ is a keyword or a modifier that causes that particular class or method to be abstract or restricted. An abstract class can only be accessed through inheritance from a different class.

Key Takeaways

  1. Abstract classes are incomplete classes that cannot be instantiated but serve as a base for others to inherit from.
  2. They contain at least one pure virtual function, meaning it has no implementation and must be defined by any derived class.
  3. Abstract classes can have member variables and member functions with implementation, but they cannot be accessed through objects of the abstract class.

Properties and Components of Abstract class in C#

Certain factors need to be there for an abstract class to be initiated or accessed. They have been listed as follows.

  1. An abstract class creates a base class commonly accessed by several derived classes. Programmers must create a derived class to use or implement an abstract class.
  2. The abstract class thus, has to be inherited by subclasses, which may even override the function.
  3. Every abstract class requires at least one abstract method.

Example of an abstract class in C#

Since the mechanism is complicated and understandable to only those familiar with the programming language, it is better to start with a simple example to help explain the concept.

Suppose a class ‘Tree’ exists, with an existing designed method known as ‘type’. Nothing else is defined, and no other particular information is provided – one does not know the kind of tree or the possible types.

If a specific example of a tree is available, then a separate class is created through the inheritance of the base class, Tree. The idea is that the new class, say Pine or Oak, cannot modify the construct or definition of the base class, that is, Tree. It has to create its class, derived from the class Tree, and define another method inherited from the base.

Advantages of abstract class in C#

  1. While sub-classes can choose to either implement or override an abstract class, certain modifications can be made that necessitate a certain function to be implemented.
  2. There is, thus, a great degree of flexibility when designing an abstract class regarding which methods must be imperatively implemented.
  3. The programmer can choose how many abstract methods they want in an abstract class. It is also possible to write the code without including abstract methods.
  4. An abstract class must be defined only once and can be accessed or derived from as many times as necessary.
  5. An abstract class is very useful when there are a set of classes which have a degree of commonality but differ in their implementation of the abstract class.

Disadvantages of abstract class in C#

  1. An abstract class can only be added to the class hierarchy if no abstract classes exist.
  2. Objects of any abstract class can only be referenced, never created.
  3. Even though abstract classes cannot be instantiated, it does not allow static variables or methods.
References
  1. https://books.google.co.in/books?hl=en&lr=&id=UbJa11wLcqYC&oi=fnd&pg=PA125&dq=Abstract+Class+in+C%23&ots=y9lmboLlHj&sig=kk5oYAJSHpmo-FyHphcAlkUh4cc&redir_esc=y#v=onepage&q=Abstract%20Class%20in%20C%23&f=false
  2. https://books.google.co.in/books?hl=en&lr=&id=-w9ftEtwPGwC&oi=fnd&pg=PP8&dq=Abstract+Class+in+C%23&ots=XXCx6-5nlm&sig=CYfs0myaTnm1anSz4QUGcZ65uS4&redir_esc=y#v=onepage&q=Abstract%20Class%20in%20C%23&f=false

Last Updated : 11 June, 2023

dot 1
/10

IT Quiz

Test your knowledge about topics related to technology

1 / 10

The intention of Machine Learning is

2 / 10

While making the text bold in Word, what do you need to do first?

3 / 10

Who founded Apple Computers?

4 / 10

What does the acronym RAM stand for?

5 / 10

Phones that offer advanced features not typically found in cellular phones, and are called

6 / 10

The main function of smart assistants like Apple Siri and Amazon Alexa is

7 / 10

A process that is repeated, evaluated, and refined is called __________

8 / 10

Firewall in computer is used for

9 / 10

'IoT' refers to

10 / 10

What does AM mean?

Your score is

0%

One request?

I’ve put so much effort writing this blog post to provide value to you. It’ll be very helpful for me, if you consider sharing it on social media or with your friends/family. SHARING IS ♥️

23 thoughts on “What is Abstract Class in C#? | Definition, Properties vs Examples”

  1. I found this post to be slightly ironic in its presentation. However, it does make the explanation quite interesting.

  2. The article nicely outlines the advantages and disadvantages. A comprehensive reference for anyone interested in learning about abstract classes in C#.

  3. This article not only highlights the advantages but also covers the disadvantages of an abstract class in C#. A thorough analysis indeed!

  4. Avatar of Richards Naomi
    Richards Naomi

    I find the flexibility of designing an abstract class very intriguing. This article presented both the pros and cons in a clear and understandable manner.

  5. I think the advantages mentioned here truly demonstrate the usefulness of an abstract class. Terrific insights provided.

Leave a Comment

Your email address will not be published. Required fields are marked *

Want to save this article for later? Click the heart in the bottom right corner to save to your own articles box!