How To Use An Abstract Class In C#
What is an abstract class in C#? This is one of the most frequently asked questions in .NET technical interviews. In this article, we will learn what an abstract class it, how to declare it, and how to use an abstract class in C#.
What is an abstract class?
An abstract class in C# provides a blueprint of what its derived classes must implement before they can use it. An abstract class contains abstract members including abstract methods, abstract properties, abstract indexers, and abstract events. All abstract members of a class must be implemented by its derived classes.
Here are the key characteristics of abstract classes,
- An abstract class cannot be instantiated.
- A class may inherit one abstract class only.
- An abstract class may contain abstract methods, properties, and events.
- An abstract class can’t be modified with the sealed class modifier.
- A derived from an abstract class must implement all inherited abstract methods and accessors
How to declare abstract methods in C#
An abstract method is declared by using the abstract modifier in a method declaration to indicate that the method is an abstract method and does not contain implementation. An abstract method is implicitly a virtual method.
Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. Derived classes of the abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method.
- public abstract void AMethod();
The implementation is provided by a method override, which is a member of a non-abstract class.
How to declare abstract properties in C#
An abstract property is declared by using the abstract modifier in a property declaration to indicate that the property is an abstract method and does not contain implementation.
How to declare an abstract class in C#?
The abstract keyword is used to create an abstract class. The class definition starts with the abstract keyword.
The following code example declares an abstract base class, Shape.
- abstract public class Shape
- {
- }
The following code example declares an abstract base class, Shape, with an abstract method and an abstract property.
- // Abstract class
- abstract public class Shape
- {
- // Abstract properties
- public abstract int Height { set; }
- // Abstract method
- public abstract int Area();
- }
How to implement an abstract class in C#?
The following code implements the Shape abstract class by inheriting two classes, Rectangle and Square, from the class. Both derived classes must implement all abstract members. In our case, both classes implement Height property and Area method.
- // Rectangle class
- public class Rectangle : Shape
- {
- private int width;
- private int height;
- public Rectangle(int height)
- {
- this.height = height;
- }
- public override int Height { set => height = value; }
- public int Width {set => width = value; }
- public override int Area()
- {
- return this.height * this.width;
- }
- }
- // Square class
- public class Square : Shape
- {
- private int width = 0;
- private int height;
- public Square(int w)
- {
- this.width = w;
- }
- public override int Height { set => height = value; }
- public override int Area()
- {
- return this.width * this.width;
- }
- }

Hemalatha SunnapuPosted Jul 6, 2018, 3:35 AM
When to use abstract class and when to use interfaces?
Hemalatha SunnapuPosted Jul 6, 2018, 3:35 AM
The same question i want to ask you
Ganapathi RamanPosted Oct 27, 2017, 1:51 AM
Nice explanation bro.. Thanks
Vithal WadjePosted Nov 26, 2013, 12:14 PM
thanks vishajeet
Vishvajeet PawarPosted Nov 26, 2013, 1:58 AM
Nice Article..Vithal...!!
Vithal WadjePosted Dec 25, 2012, 7:13 AM
thank you charinda nanayakkara sir,it my pleasure
Vithal WadjePosted Dec 25, 2012, 7:12 AM
Thank you sarath kumar sir
charinda nanayakkaraPosted Dec 25, 2012, 6:30 AM
nice simple article.thanx.
sarath kumarPosted Dec 21, 2012, 6:58 AM
Hi thanks for your article .. But i am having doubt . How the abstract class will work in asp.net?? Whatever the class we can create object for that directly know.. Whether it will improve in performance???
Vithal WadjePosted Dec 10, 2012, 4:52 AM
Thank you akhil sir for reading my article, this is a very good question now i am preparing article on that since last few days once it complted i will personally inform you. if you have suggestion regarding my current artilce then contact me i will improve it
Akhil MittalPosted Dec 10, 2012, 12:09 AM
When to use abstract class and when to use interfaces????
Vithal WadjePosted Dec 6, 2012, 2:02 AM
adil sir ,refer examples from above
Adil AnsariPosted Nov 28, 2012, 7:48 AM
thanks for this article, but i am still confused that in scenario where i need "common fields and members to all sub-classes" i can just use the non abstract class instead of the abstract class and also if implementation of the some of the member in my class isn't possible, I can make them virtual in non abstract class and can overwrite it later on in sub-classes. So when, where and why we should use the Abstract class ?? Any clarification would be appreciated!
Vithal WadjePosted Nov 28, 2012, 1:47 AM
ok sir,i will try to write as soon as possible
Rohatash KumarPosted Nov 28, 2012, 12:34 AM
waiting ...Vithal
Vithal WadjePosted Nov 27, 2012, 11:34 PM
yes Rohatash sir ,i will write a new article on it becaue it deep concept and in some cases Abstract Base Classes fail in versioning
Rohatash KumarPosted Nov 27, 2012, 10:24 PM
Vithal. can you define versioning with abstract class?