Introduction to Abstract Class
Every time I read about a Technical Interview, one question is always out there. What is the difference between an Abstract Class and an Interface and why would you use one over the other? It seems like a simple question but most often, the answer to this question seems insufficient for the interviewer and can cost a job. So, I decided to write a series of articles about them and have tried to explain it in as simple a manner as I can.
- What: What is it?
- Why: Why do I need to learn it?
- How: How can I work with it?
I used the same approach to write this article. This is the first article of the series and focus on Abstract Class. In my next article, I'll talk about Interface.
Here is the road map of the series of articles under the title "Abstract Class & Interface: Two Villains of Every Interview".

Abstract Class
What is an Abstract Class?
The dictionary meaning of the word "abstract" is a thought or idea that has no physical existence and is just conceptual. We can use the idea to build something of a physical existence. In the MSDN Library, the "abstract" keyword indicates that the thing has a missing or incomplete implementation and must be completed by others.
The abstract keyword can be used with classes, methods, properties, indexers and events. If we use the abstract keyword with a class, it indicates that the class is intended to be a base class and can have abstract methods (ideas) that must be implemented in a derived class (physical existence).
An abstract class is a special kind of class that has no implementation. It cannot be instantiated. Its implementation logic is provided by the classes that derive from it. It can have both abstract as well as non-abstract methods.
It is not compulsory to have only abstract methods in an abstract class. We can also have an abstract class with only non-abstract methods.
Why do we need an Abstract Class?
With an Abstract Class, we can provide some kind of default functionality for all derived classes to extend from. This is useful to avoid code duplication in many cases.
Suppose we are defining an iPhone class for Apple and then inheriting it to iPhone5 and iPhone5s subclasses. Practically we don't want an object of an iPhone class since we first need to know the model of iPhone. So, the iPhone class should be an abstract class that contains some predefined functions like Call() and SMS() for all iPhone models to share . We can also add abstract methods like Model() and Color() into the iPhone class that must be implemented by all the subclasses inheriting iPhone. The main advantage of this approach is, whenever we inherit the iPhone class into a derived class, say iPhone5s, we need not define the Call() and SMS() methods again. We just need to implement the abstract methods and we are good to go. It helps to provide default functionality in all the derived classes and also avoids code duplication.
Abstract classes are also useful in the case of modifications to the project. If you plan on updating the base class in your project, it is better to make the class abstract. Because you can define a functionality in an abstract base class and automatically all the inheriting classes will have the same functionality without disturbing the hierarchy.
How to define an Abstract Class
As we have discussed earlier, classes can be declared as abstract by putting the keyword abstract before the class definition. So, let's get started with Abstract Class by using a simple console application.
Create a console application project in Visual Studio and name it "AbstractClassDemo".

By default, it gives a class named Program with Main method in it for code execution. We can create an abstract class by putting the keyword abstract before a class definition as follows:
- using System;
- namespace AbstractClassDemo
- {
- abstract class iPhone { } //Definition of an Abstract Class
- class Program
- {
- static void Main(string[] args) { }
- }
- }
- using System;
- namespace AbstractClassDemo
- {
- abstract class iPhone { }
- class Program
- {
- static void Main(string[] args)
- {
- //Instantiation of an Abstract Class
- iPhone iphone = new iPhone();
- }
- }
- }
So, we need to define members in it that can be in derived classes. We can define abstract as well as non-abstract members in an abstract class. An abstract class with non-abstract method is as follows:
- using System;
- namespace AbstractClassDemo
- {
- abstract class iPhone
- {
- //Non-Abstract Method
- public void Call()
- {
- Console.WriteLine("Call Method: This method provides Calling features");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- }
- }
- }
The iPhone class shows a non-abstract method Call() that provides the default functionality to all sub classes that are derived from it. We cannot create an object of iPhone class but we can still use the Call() method in derived classes.
- using System;
- namespace AbstractClassDemo
- {
- abstract class iPhone
- {
- //Non-Abstract Method
- public void Call()
- {
- Console.WriteLine("Call Method: This method provides Calling features");
- }
- }
- class Program: iPhone
- {
- static void Main(string[] args)
- {
- //Instance Creation of Derived Class
- Program program = new Program();
- program.Call();
- Console.ReadKey();
- }
- }
- }
The code above shows a simple inheritance of an abstract class into a concrete class. This type of inheritance can also be done by two concrete classes. So, why do we want an abstract class?
The answer is, to provide default functionality and to add abstract methods. The iPhone class is inherited by all iPhone models, so the Call() method is required in all the models. It is better to define a Call() method in the abstract class so that each derived class can have the Call() method automatically and doesn't need to define it again.
Each iPhone model has some of its own features like Color and Model. So, we can define a contract in an abstract class that must be implemented in derived classes as per their requirements. These types of contracts are called abstract methods and in this example is Model(). Abstract methods only have a signature and no implementation. It is a kind of contract that forces all the subclasses to implement it.
Like the abstract class, abstract methods are also declared using the abstract keyword. It may be noted that an abstract method cannot be private or it gives an error:
- using System;
- namespace AbstractClassDemo
- {
- abstract class iPhone
- {
- //Non-Abstract Method
- public void Call()
- {
- Console.WriteLine("Call Method: This method provides Calling features");
- }
- //Abstract Method kept as Private
- abstract void Model();
- }
- class Program
- {
- static void Main(string[] args)
- {
- }
- }
- }

So, the correct way to declare an abstract method is as follows:
- using System;
- namespace AbstractClassDemo
- {
- abstract class iPhone
- {
- //Non-Abstract Method
- public void Call()
- {
- Console.WriteLine("Call Method: This method provides Calling features");
- }
- //Abstract Method
- public abstract void Model();
- }
- class Program
- {
- static void Main(string[] args)
- {
- }
- }
- }
- using System;
- namespace AbstractClassDemo
- {
- abstract class iPhone
- {
- //Non-Abstract Method
- public void Call()
- {
- Console.WriteLine("Call Method: This method provides Calling features");
- }
- //Abstract Method
- public abstract void Model();
- }
- class iPhone5s: iPhone
- {
- }
- class Program
- {
- static void Main(string[] args)
- {
- }
- }
- }

Ok. Let's provide the definition of Model() method in the derived class:
- using System;
- namespace AbstractClassDemo
- {
- abstract class iPhone
- {
- //Non-Abstract method
- public void Call()
- {
- Console.WriteLine("Call Method: This method provides Calling features");
- }
- //Abstract Method
- public abstract void Model();
- }
- class iPhone5s: iPhone
- {
- //Abstract Method Implementation
- public void Model()
- {
- Console.WriteLine("Model: The model of this iPhone is iPhone5s");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- }
- }
- }

The error says "The Model() method is not implemented in derived class". It seems fair since we aren't overriding the base method, which means the compiler believe that there is no implementation of the Model() method in the derived class.
It also gives us a warning "To make the current member override that implementation, add the override keyword, otherwise add the new keyword". It means that the compiler is confused about the Model() method we declared in the iPhone5s class.
If you want to override the base class method in derived class, use the override keyword with the method and if your derived class method is not related in any way with the base class method, use the new keyword. The new keyword signifies that the method in the derived class has nothing to do with the base class method.
In our case, we want the base class method to be defined in the derived class. So, we use the override keyword. Also, we can add local methods in the iPhone5s class:
- using System;
- namespace AbstractClassDemo
- {
- abstract class iPhone
- {
- //Non-Abstract Method
- public void Call()
- {
- Console.WriteLine("Call Method: This method provides Calling features");
- }
- //Abstract Method
- public abstract void Model();
- }
- class iPhone5s: iPhone
- {
- //Abstract Method Implementation
- public override void Model()
- {
- Console.WriteLine("Model: The model of this iPhone is iPhone5s");
- }
- //Derived Class Local Method
- public void LaunchDate()
- {
- Console.WriteLine("Launch Date: This iPhone was launched on 20- September-2013");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- }
- }
- }
- using System;
- namespace AbstractClassDemo
- {
- abstract class iPhone
- {
- public void Call()
- {
- Console.WriteLine("Call Method: This method provides Calling features");
- }
- public abstract void Model();
- }
- class iPhone5s: iPhone
- {
- public override void Model()
- {
- Console.WriteLine("Model: The model of this iPhone is iPhone5s");
- }
- public void LaunchDate()
- {
- Console.WriteLine("Launch Date: This iPhone was launched on 20-September-2013");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- iPhone5s iphone5s = new iPhone5s();
- iphone5s.Call();
- iphone5s.Model();
- iphone5s.LaunchDate();
- Console.ReadKey();
- }
- }
- }

In the preceding example, I explained how to use an abstract class in a very simple way. We are able to implement an abstract class and its abstract members into a concrete class. The following are some key points to be remembered when working with abstract classes.
Key Points
- We cannot create an object of Abstract Class but we can create a reference of it.
- using System;
- namespace AbstractClassDemo
- {
- abstract class absClass { }
- class Program
- {
- public static void Main(string[] args)
- {
- //We can't do this
- //absClass cls = new absClass();
- //We can do this
- absClass cls;
- }
- }
- }
- An inheritance between abstract to abstract classes is possible. We don't need to implement abstract methods of the base abstract class into a derived abstract class. We can implement it later in concrete classes.
- using System;
- namespace AbstractClassDemo
- {
- abstract class absClassA
- {
- //Abstract Method
- public abstract void SomeMethod();
- }
- abstract class absClassB: absClassA //Abstract to Abstract Inheritance
- {
- }
- class Program: absClassB
- {
- public override void SomeMethod()
- {
- //Some Implementation Here
- }
- public static void Main(string[] args)
- {
- }
- }
- }
- An abstract class can never be sealed or static.
- An abstract class can have abstract as well as non abstract methods.
- The abstract keyword can be used with class, methods, properties, indexers and events.
- Abstract members can only be declared inside an abstract class.
- An abstract member cannot be static or private.
- An abstract method cannot be marked virtual.
- A concrete class cannot inherit more than one abstract class, in other words multiple Inheritance is not possible.
- Without an abstract class, we cannot implement the Template Method Pattern.
Conclusion
I hope this article helps you to understand the various possibilities of Abstract Classes. In Part 2 of my articles I'll talk about Interface and the difference between Abstract Class and Interface. Your feedback and constructive criticism is always appreciated, keep it coming. Until then, try to put a ding in the universe!

guru prasanthPosted Oct 17, 2021, 12:31 PM
Why can't we use concrete class for inheriting methods instead of abstract class. Even if we use abstract class, we need to write entire method name (access modifier, [override], return type, method name) in base class. Also it is a mandatory thing to implement abstract methods. But if I use concrete class, I can implement whatever methods I want right. There is some flexibility. Can you please explain with better examples?
anil kumarPosted Jul 11, 2021, 9:19 AM
This article made my day. many thanks
Arun Kumar SinghPosted Feb 15, 2021, 1:16 PM
Great Sahil Sharma nice explanation.
Haris ZiaPosted Nov 15, 2020, 2:29 PM
Thank you so much for posting this article. very very helpful.. beautiful explanation and very easy to understand
Ram MPosted Sep 16, 2020, 8:53 AM
Thank you very much much. very helpful article. and the best part is very easy to understand.
Rahul Pushpendu BhaskarPosted Sep 25, 2019, 12:19 PM
Very good n well explained..
Bidyasagar MishraPosted Aug 2, 2019, 10:40 AM
Mind-blowing article
kamini sharmaPosted Jul 7, 2019, 4:23 AM
Thank you :) You explained it really well. If possible please post the article on dependency injection also.
Shruti SikchiPosted May 11, 2019, 7:36 AM
Thank You! Keep posting such articles!
Dharmendra kuma yadavPosted Jan 23, 2019, 11:26 AM
Very nice and simple to understand
sagar modiPosted Dec 31, 2018, 9:36 AM
Thank you so much sahil...Superb explanation in easy way to clear the concept
Haris ZiaPosted Oct 22, 2018, 4:00 PM
I must say thank you soo much. its a great and very helpful article. and the best part is very easy to understand.
Kishor MaliPosted Sep 22, 2018, 7:19 AM
Superb Article...Very Nice
Bhramara MGPosted Jul 2, 2018, 8:27 AM
Amazing, the concepts are well explained. It's refreshing to read these concepts.
Amit Kumar PalPosted Jun 26, 2018, 9:13 AM
Nice article...helpfull
Prerna ChaturvediPosted May 7, 2018, 6:14 AM
What is advantage of abstract class over a normal class
Akbar MullaPosted Mar 14, 2018, 10:43 PM
Excellent explanation
Fu TilPosted Feb 1, 2018, 4:51 AM
Awesome, sounds like someone who knows his craft
Pardha GoudPosted Jan 5, 2018, 11:57 PM
Thanq very much!!!
Vishal PrajapatiPosted Jan 5, 2018, 4:08 AM
Awesome sir... you just nailed it by your explanation...
AKHILESH deetiPosted Jan 1, 2018, 11:41 PM
Awesome article. I understood the concept now.
Renjith JPosted Dec 1, 2017, 1:27 PM
Excellent way to describe Abstract class...
Zeeshan SattarPosted Sep 10, 2017, 6:14 AM
Love u :***** after 8 months i finally understood wtf this abstract class is :) your example made everything clear like water :)
Guest UserPosted Aug 19, 2017, 3:15 AM
Wow very nice article
Niranjan PoddarPosted Jun 29, 2017, 4:41 AM
Nice article....................
Naveen BishtPosted May 26, 2017, 8:13 AM
Good article.. I have question can abstract class can have constructor if yes, then we can't create the instance of abstract class then what is use of constructor in abstract class.
Mazhar PashaPosted May 26, 2017, 3:36 AM
Very good article...good explanation and Thanks for sharing....
Denil ParmarPosted Mar 24, 2017, 8:33 PM
Good article with a good example... I came across many articles on the internet, but this example made my concept very clear.
Rohit PatelPosted Mar 16, 2017, 10:09 PM
Very nice Explaination..
gajanan thatePosted Mar 9, 2017, 3:27 AM
Very nice and easy to understand article..........keep posting.....Sahil u rocks bro...
Mohammed NadeemPosted Feb 24, 2017, 1:23 PM
My confusion about abstract classes will now REST IN PEACE . Beautiful explaination with gorgeous example!!!!!!!!!!
neharaPosted Feb 16, 2017, 9:12 AM
Very helpful artical with nice explanation wth example
Shrikant DubeyPosted Feb 5, 2017, 8:34 AM
very nice explanation.
Manav PandyaPosted Dec 26, 2016, 8:26 AM
Thanks for sharing this interview stuff sir
Dharmesh BaraskarPosted Dec 6, 2016, 1:42 AM
Thank you Sahil Sharma that so nice and easy to understand
singaraj GPosted Oct 6, 2016, 9:15 PM
Nice one
Soumalya DasPosted Aug 11, 2016, 6:03 AM
Really good one
Rahul Pushpendu BhaskarPosted Jul 28, 2016, 1:56 AM
Please Go Ahead...
Rahul Pushpendu BhaskarPosted Jul 28, 2016, 12:49 AM
Good one...
Prasanna MuraliPosted Jul 27, 2016, 9:41 PM
Nice share
RajaPosted Jul 27, 2016, 9:03 AM
Good One...
Bikesh SrivastavaPosted Jul 27, 2016, 5:16 AM
Good explanation..
Sajeed SkPosted Jul 26, 2016, 10:31 AM
Hi Shail, Thanks for clearing the concept explained very well, It was often asked in interviews and will you please clear why can't we use static keyword infront of abstract class and its methods >\??
shailesh voraPosted Jul 26, 2016, 4:16 AM
Thanks for sharing such a nice article.
Karthik SivakumarPosted Jul 20, 2016, 3:50 AM
Nicely Explained!!!
Gaurav KumarPosted Jul 20, 2016, 12:43 AM
Thanks, one question I want to ask that it is a compulsion that you must implements all the non-abstract methods in derived class as we do with abstract methods?
sagar SHINGANPosted May 13, 2016, 2:16 AM
nice article
Robert JamesPosted May 12, 2016, 2:50 PM
This is Cool
neeraj kanojiyaPosted May 5, 2016, 2:57 AM
Good Job.. Explanation is very good.
Sun LightPosted Apr 19, 2016, 12:23 PM
very nice explanation..
Ajeet MishraPosted Feb 27, 2016, 4:24 AM
Very nice
mitesh prajapatiPosted Feb 5, 2016, 4:24 AM
Now i got a clear picture about Abstract class and it use. Well and practically explained. Thanks.. Keep it up dear
Ravinder RaturiPosted Jan 12, 2016, 5:39 AM
Hi Sahil, Well explained. I really like and it helped me a lot in understanding the concept. Great work, keep it up buddy. Thanks
Jaipal ReddyPosted Jan 12, 2016, 12:31 AM
Good article. .
prabhuPosted Dec 18, 2015, 7:13 AM
unexpected article. Good one.
Vishal JadavPosted Dec 7, 2015, 3:06 AM
Very easy to understand.
bhanu shrivastavaPosted Nov 19, 2015, 6:44 AM
Excellent explanation nice job :)
David SmithPosted Oct 8, 2015, 8:24 AM
Good explanation
Sachin ChakolePosted Oct 1, 2015, 4:12 PM
I completely Understand
Sachin ChakolePosted Oct 1, 2015, 4:12 PM
Superb
sushil kumarPosted Sep 22, 2015, 5:47 AM
Aspmaterials.blogspot.com ....
sushil kumarPosted Sep 22, 2015, 5:46 AM
Nice explain..
Upendra Pratap ShahiPosted Sep 8, 2015, 8:55 AM
nice explain sir
harish kasurdePosted Aug 27, 2015, 8:01 AM
Nice explanation..Greate
Mahesh VekariyaPosted Aug 20, 2015, 4:53 AM
Excellent Explanation... Good Job Sahil..keep it up... :)
Tanni SinghPosted Aug 15, 2015, 1:35 PM
Good explained.
Fahad MirzaPosted Aug 7, 2015, 8:13 AM
Excellent Explanation... Good Work Sahil... :) keep it up...
Rajendra TaradalePosted Jun 29, 2015, 11:13 AM
Nice explanation...
Anil KumarPosted Jun 16, 2015, 2:24 AM
Great Explanation...Thankyou..
Farhan ShariffPosted Jun 10, 2015, 9:20 AM
Excellent way of explaining thank you sahil
Rajeesh MenothPosted Jun 10, 2015, 7:32 AM
good article sahil .. :)
Santosh YadavPosted May 29, 2015, 3:00 PM
greate article. Thanks
Gowtham RajamanickamPosted May 20, 2015, 1:18 AM
thanks
Gowtham RajamanickamPosted May 20, 2015, 1:18 AM
good one
Prakash KaruppiahPosted May 19, 2015, 2:21 AM
Thanks very nice..
Ansil AnsarPosted May 8, 2015, 3:07 AM
good, very well explained, Thanks sahil
Vinod TGPosted May 2, 2015, 7:59 AM
Awesome :)
Manikandan SelvarajPosted Apr 21, 2015, 4:08 AM
Easy to understand in clear manner.Thanks sir
Anita prabhuPosted Mar 27, 2015, 7:09 AM
Simple, clean and helpful for practically understanding the concepts.Thanks a lot :) Keep posting,.
Suresh SundarPosted Mar 12, 2015, 1:25 AM
clean and clear, good effort Sahil. keep posted ;)
praveen bunkarPosted Mar 12, 2015, 12:55 AM
very nice article ! thanks!
Rahul SinghPosted Dec 17, 2014, 12:24 AM
Very well explained. Thanks!
srini ajPosted Dec 3, 2014, 4:42 AM
Clear explanation with good examples.
Jaganathan BantheswaranPosted Nov 6, 2014, 1:13 AM
Nice writeup :) keep it up :)
Awadhendra TripahtiPosted Nov 3, 2014, 2:32 AM
Nice :)
Elima TripathyPosted Oct 17, 2014, 12:06 PM
Very nice article...good effort
Anand MorePosted Oct 14, 2014, 4:46 AM
very clean description in easy way,great !!
Shilpa SoniPosted Oct 10, 2014, 10:54 PM
As you said in keypoints #1 that we cannot create object of abstract class but can create a reference of it. So will my following code work: iPhone obj; obj = new iPhone5(); obj.Call(); obj.Model(); obj.LaunchDate(); //I'm getting compile time error here. Can you please explian why?
krishna moorthiPosted Sep 24, 2014, 12:21 PM
good
Mukesh ChauhanPosted Sep 24, 2014, 2:34 AM
nice article.
Rohatash KumarPosted Sep 24, 2014, 12:17 AM
Nice article shahil. I learnt lot from your article.
Manju lata YadavPosted Sep 23, 2014, 10:59 AM
nice article, I liked the villan
JUKE BOXPosted Sep 16, 2014, 3:43 PM
nyc article
Pankaj BajajPosted Sep 11, 2014, 10:16 AM
nice article Sahil.
Abhishek JaiswalPosted Sep 10, 2014, 1:42 PM
Good Points, good article! :)
Nikhil SinghPosted Sep 10, 2014, 12:01 PM
Hey Sahil, it is nice article . Thanks a lot ......
Nimit JoshiPosted Sep 10, 2014, 3:57 AM
Very Great...
Rahul BansalPosted Sep 10, 2014, 3:02 AM
Really informative .....good job Sahil...:)
Gursewak SinghPosted Sep 10, 2014, 1:43 AM
Great work #SahilSharma.
Former memberPosted Sep 9, 2014, 11:57 PM
Nice article ! there was no mistake in grammar and usage of articles were great. Gona tweet this article ! Great job Sahil Sharma !
Guest UserPosted Sep 9, 2014, 10:54 PM
good one.
Sandeep Singh ShekhawatPosted Sep 9, 2014, 10:38 PM
Article title is giving negative sense in respect to technology. Author should n't create a fear to read his article. It should be "Abstract Class & Interface - Two Most Important Concept in Every Interview". Each concept helps to improve existing facility and help to develop functionality in software. If someone helps you then how could be "Villains". It seems that someone is abusing in some concepts.