What is Abstract Class in C#? | Definition, Properties and 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.

/10

IT Quiz

Test your knowledge about topics related to technology

1 / 10

The conductivity of semiconductor materials

2 / 10

Which of the following AI domain attempts to extract information from spoken and written words using algorithms?

3 / 10

'.MOV' extension usually refers to what kind of file?

4 / 10

Who founded Microsoft?

5 / 10

Which web browser is developed by the Google

6 / 10

What was the name of the space shuttle that landed man on the moon?

7 / 10

Who is considered as the father of computing

8 / 10

Which American Computer Company is also known by the nick name "Big Blue"

9 / 10

Which of these is not a social media platform?

10 / 10

Which of the following is not a search engine

Your score is

0%

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
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 ♥️

Leave a Comment

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