You are on page 1of 29

1/28/2021 When To Use Abstract Class and Interface In Real Projects

When To Use Abstract Class andBecome a member


C# Corner
Interface Login
In Real
Projects Post Ask Question

Debendra Dash Updated date Sep 04, 2016

564.7k 99 109

Download Free .NET & JAVA Files API


Try Free File Format APIs for Word/Excel/PDF

Introduction

This is now an important question asked by an interviewer in the interview. I have found many
solutions on the Internet regarding "When to use interface and abstract method?". Lately, I
thought about writing an article on simplifying my experience and what I learned. If you check
both the things, they seem to be very similar, which makes a confusion in the mind while
answering these questions.

Here, in this article, I will try to explain these things.

Abstract Class

Before giving the introduction about abstract class, I will try to explain the abstract method,
rst.

Abstract Method

A method without any particular body is known as an abstract method. These methods contain
only declaration of the method.To declare an abstract method, we have to use abstract
modi er on the method.The class, under which these abstract methods are de ned, is referred
to as an abstract class, and this also has to be declared using abstract modi er.The
implementation of abstract method is done by a derived class. When the abstract class inherits
the derived class, the derived class must implement the abstract methods using override
keyword in it.

Abstract Class An abstract class is a special class that contains both abstract and non-abstract
members in it.

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 1/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

Example
Become a member
C# Corner Login
01. public abstract class Cars
02. { Post Ask Question
03.
04. //Abstract Methods
05. public abstract double price();
06. public abstract int getTotalSeat();
07. }

 Here are some points regarding abstract class.

1. Abstract class can contain abstract members as well as non-abstract members in it.
2. A class can only inherit from one abstract Class.
3. We cannot create object of an abstract class.

Interface

It is also user de ned type like a class which only contains abstract members in it. These
abstract members should be given the implementation under a child class of an interface. A
class can be inherited from a class or from an interface.

Points to remember

1. Interface contains only abstract methods.


2. We cannot create object of an interface.
3. The default scope for a member in Interface is Public. So, no need to use the Public access
speci er in the program.

NOTE - In case of multiple inheritance, use Interface.


 
From both the de nitions, it gets concluded that,

Figure 1

Figure 2

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 2/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

Become a member
C# Corner Login

Post Ask Question

 
Now, I have   class like Hyundai and Toyota which are derived from a parent class called Car.
Here, I have the car methods as follow:

01. public class Cars


02. {
03.
04. public string Wheel()
05. {
06. return "4 wheeler";
07.
08. }
09. public string CheckAC()
10. {
11. return "AC is available";
12. }
13. public string CallFacility()
14. {
15. return "Call Facility supported";
16. }
17.
18. }

Here, I have 2 types of cars which are inherited from Cars.

01. using oops1;


02. using System;
03. using System.Collections.Generic;
04. using System.Linq;
05. using System.Text;
06. using System.Threading.Tasks;
07.
08. namespace oops1
09. {
10. public class Hyundai : Cars
11. {
12.
13. static void Main(string[] args)
https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 3/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

14. {
15. Hyundai dust = new Hyundai(); Become a member
C# Corner Login
16.
17. Console.WriteLine(dust.CallFacility());
Post Ask Question
18. Console.WriteLine(dust.Wheel());
19. Console.WriteLine(dust.CheckAC());
20. Console.ReadLine();
21.
22.
23. }
24. }
25.
26.
27. }

Now, it runs as expected and gives the following output .

Similarly, as Toyota is a car and it also inherits from Cars class.

01. using System;


02. using System.Collections.Generic;
03. using System.Linq;
04. using System.Text;
05. using System.Threading.Tasks;
06.
07. namespace oops1
08. {
09. public class Toyota : Cars
10. {
11. public string DiscountPrice()
12. {
13. return "20% discount on buying Toyoya Cars";
14. }
15.
16.
17. static void Main(string[] args)
18. {
19. Toyota Toy = new Toyota();
20.
21. Console.WriteLine(Toy.CallFacility());
22. Console.WriteLine(Toy.Wheel());
23. Console.WriteLine(Toy.CheckAC());
https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 4/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

24. Console.WriteLine(Toy.DiscountPrice());
25. Become a member
C# Corner Login
26. Console.ReadLine();
27. Post Ask Question
28.
29. }
30. }
31. }

We need some methods which are common to both the classes but their implementation is
di erent.

PRICE()- Both have price but di erent price.


TotalSeat()- Both have total seats but di erent no. of seats.
colors()- Both cars are of di erent colour. 

So, here are the options for implementing these methods in both classes.

Can I go for a normal class?


Should I use interface here?
Can I use an abstract class?

If we are taking class, then we can only write normal methods having common
implementation there. But, this will not satisfy our requirement because we need
separate implementations in both the classes. Thus, we will not go forward with Class.

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 5/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

If we go for interface, we can achieve our goal but it will be like this. First, I will declare n
Become a member
C# Corner Login
interface, as follows.
Post Ask Question
01. interface IExtra
02. {
03. double price();
04. int getTotalSeat();
05. string colors();
06. }

Now, let me rst inherit the Toyota class from Cars class and IExtra interface class to achieve
our goal.

The code is given below.

01. namespace oops1


02. {
03. public class Toyota : Cars,IExtra
04. {
05. public string DiscountPrice()
06. {
07. return "20% discount on buying Toyoya Cars";
08. }
09. public double price()
10. {
11. return 1000000.00;
12. }
13. public int getTotalSeat()
14. {
15. return 5;
16. }
17. public string colors()
18. {
19. return "Red";
20. }
21.
22. static void Main(string[] args)
23. {
24. Toyota Toy = new Toyota();
25.
26. Console.WriteLine(Toy.CallFacility());
27. Console.WriteLine(Toy.Wheel());
28. Console.WriteLine(Toy.CheckAC());
29. Console.WriteLine(Toy.DiscountPrice());
30. Console.WriteLine("Total ONRoad Price:"+ Toy.price());
31. Console.WriteLine(Toy.getTotalSeat());
32. Console.WriteLine(Toy.colors());
33.
34. Console.ReadLine();
35.
36.
37. }

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 6/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

38. }  
Become a member
C# Corner Login

Post Ask Question

So, the sketch diagram of this implementation will be like this.

Abstract Class
 
Now, we will see here how we can solve our problem using Abstract Class.

1. De ne an abstract class as Car. 

2. Put all the common functionality in simple methods and all the methods whose
implementation is di erent but name is same. Make them  Abstract method.

Here is my Car abstract class.

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 7/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

Become a member
C# Corner Login

Post Ask Question

01. using System;


02. using System.Collections;
03. using System.Collections.Generic;
04.
05. namespace oops1
06. {
07. public abstract class Cars
08. {
09.
10. //put all the common functions but diffrent implementation in abs
11. public abstract double price();
12. public abstract int getTotalSeat();
13. public abstract string colors();
14.
15. //put all the common property in normal class
16. public string Wheel()
17. {
18. return "4 wheeler";
19.
20. }
21. public string CheckAC()
22. {
23. return "AC is available";
24. }
25. public string CallFacility()
26. {
27. return "Call Facility supported";
28. }
29. }
30. }
https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 8/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

Become a member
C# Corner Login
Now, here is my Toyota class which is derived from Cars abstract class.
Post Ask Question
01. using System;
02. using System.Collections.Generic;
03. using System.Linq;
04. using System.Text;
05. using System.Threading.Tasks;
06.
07. namespace oops1
08. {
09. public class Toyota : Cars
10. {
11. public string DiscountPrice()
12. {
13. return "20% discount on buying Toyoya Cars";
14. }
15. public override double price()
16. {
17. return 1000000.00;
18. }
19. public override int getTotalSeat()
20. {
21. return 5;
22. }
23. public override string colors()
24. {
25. return "Red";
26. }
27.
28. static void Main(string[] args)
29. {
30. Toyota Toy = new Toyota();
31. Console.WriteLine("-------
Common property defined commonly in Cars Class----------");
32.
33. Console.WriteLine(Toy.CallFacility());
34. Console.WriteLine(Toy.Wheel());
35. Console.WriteLine(Toy.CheckAC());
36. Console.WriteLine("-------
Own property defined in Toyota class------------");
37. Console.WriteLine(Toy.DiscountPrice());
38. Console.WriteLine("-------
Common method but implementation is diffrent defined in IExtra Interface-
-----------");
39. Console.WriteLine("Total ONRoad Price:"+ Toy.price());
40. Console.WriteLine(Toy.getTotalSeat());
41. Console.WriteLine(Toy.colors());
42.
43. Console.ReadLine();
44.
45.
46. }
https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 9/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

47. }
48. Become a member
C# Corner Login
49.
50. } Post Ask Question

And thus, the result will be the same.

 
Conclusion

When we have the requirement of a class that contains some common properties or methods
with some common properties whose implementation is di erent for di erent classes, in that
situation, it's better to use Abstract Class then Interface.

Abstract classes provide you the exibility to have certain concrete methods and some other
methods that the derived classes should implement. On the other hand, if you use interfaces,
you would need to implement all the methods in the class that extends the interface. An
abstract class is a good choice if you have plans for future expansion.
 
Now, I will explain the second use of Abstract Class here. 
 
Imagine, we have taken a normal parent class as Cars where we have only de ned the
common methods. And, my Toyota class is derived from the Cars class. This will look like the
below code.

01. public class Cars


02. {
03.
04. public string Wheel()
https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 10/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

05. {
06. return "4 wheeler"; Become a member
C# Corner Login
07.
08. } Post Ask Question
09. public string CheckAC()
10. {
11. return "AC is available";
12. }
13. public string CallFacility()
14. {
15. return "Call Facility supported";
16. }
17.
18. }

 And, here is the Toyota class after inheriting from Cars.

01. using System;


02. using System.Collections.Generic;
03. using System.Linq;
04. using System.Text;
05. using System.Threading.Tasks;
06.
07. namespace oops1
08. {
09. public class Toyota : Cars
10. {
11. public string DiscountPrice()
12. {
13. return "20% discount on buying Toyoya Cars";
14. }
15.
16.
17. static void Main(string[] args)
18. {
19.
20.
21. Console.ReadLine();
22.
23.
24. }
25. }
26.
27.
28. }

Here, users know only the Toyota class, its methods, and properties. The user can access the
Cars class property and method by creating the object of Toyota class because it is a child class.

Now, the main demerit of this kind of Implementation is that it allows the user to create the
object of the Parent class (which the user should not be aware of and this is not his strategy).
Let's see what happens when the user creates the object.

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 11/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

01. using System;


02. using System.Collections.Generic; Become a member
C# Corner Login
03. using System.Linq;
04. using System.Text; Post Ask Question
05. using System.Threading.Tasks;
06.
07. namespace oops1
08. {
09. public class Toyota : Cars
10. {
11. public string DiscountPrice()
12. {
13. return "20% discount on buying Toyoya Cars";
14. }
15.
16.
17. static void Main(string[] args)
18. {
19. //Toyota Toy = new Toyota();
20. Cars ca = new Cars();
21. Console.WriteLine("-------
Common property defined commonly in Cars Class----------");
22.
23. Console.WriteLine(ca.CallFacility());
24. Console.WriteLine(ca.Wheel());
25. Console.WriteLine(ca.CheckAC());
26.
27. Console.WriteLine("-------
Own property defined in Toyota class------------");
28.
29. Console.WriteLine(ca.DiscountPrice());
30.
31.
32. Console.ReadLine();
33.
34.
35. }
36. }
37.
38.
39. }

As we know, we won't be able to access the child class methods using parent class object, this
Cars object will not access the DiscountPrice() Method because it is a child class method. This
implementation will not satisfy our requirement. So, even if the user will create an object of
Parent class, we need to check them because using that object, he can't able to access the child
class method.

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 12/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

Become a member
C# Corner Login

Post Ask Question

So, the main problem we face here is,

As the user is unaware of parent class, he is able to create the object of parent class and
unable to access the child class method using parent class. So, to restrict this, we have to do
the following things-

We should not give the permission to create the object of parent class in child class
We can only allow the user to create the child class object to access both parent class
methods and child class methods.

Simple to do this Create the base class as an Abstract class and that will solve our
problems as follow.

01. using System;


02. using System.Collections;
03. using System.Collections.Generic;
04.
05. namespace oops1
06. {
07. public abstract class Cars
08. {
09.
10.
11. public string Wheel()
12. {
13. return "4 wheeler";
https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 13/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

14.
15. } Become a member
C# Corner Login
16. public string CheckAC()
17. { Post Ask Question
18. return "AC is available";
19. }
20. public string CallFacility()
21. {
22. return "Call Facility supported";
23. }
24.
25.
26.
27.
28.
29. }
30.
31.
32. }

Even if you want to create the object of parent class, it shows the following error.

So, this will solve our problem.

Conclusion

So, in those cases, we will use abstract class where we want to restrict the user from creating
the object of parent class because by creating object of parent class, you can't call child class
methods. So, the developer has to restrict accidental creation of parent class object by de ning
it as abstract class.

So, I think, in these ways, we can use abstract class in our real time project.

When To Use Interface In Real Time Project?


 
INTERFACE

It is also user-de ned type like a class which only contains abstract members in it and these
abstract members should be given implementation under a child class of an interface. A class
can inherit from a class or from an interface.
https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 14/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

01. interface I1
02. { Become a member
C# Corner Login
03. void MethodToImplement();
04. } Post Ask Question

In your daily programming, you are using a lot of interfaces knowingly or unknowingly. If you
are using a List, Dictionary, or Hash Table etc in your project, then you are indirectly using
interface in your project because all collections are derived from an Interface, IEnumerable.
Here, I will not discuss about the uses of Interface. I will discuss when can we use our interface
in C#.

      Before explaining in details, I will ask you to focus on the following points.

In C++, we have a concept of Multiple Inheritance where we nd a serious problem called


Diamond Problem.

Thus, in C#, multiple inheritance is not supported. When there is a situation like multiple
inheritance, use Interface.

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 15/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

Become a member
C# Corner Login

Post Ask Question

The solution of the multiple inheritance can be provided by Interface. So, we can do this
example using Interface as follows. In this way, the diamond problem of Inheritance is solved.

01. using System;


02. using System.Collections.Generic;
03. using System.Linq;
04. using System.Text;
05. using System.Threading.Tasks;
06.
07. namespace OOPs2
08. {
09. interface Demo1
10. {
11. void print();
12.
13. }
14. interface Demo2
15. {
16. void print();
17.
18. }
19.
20.
https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 16/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

21. class Program:Demo1,Demo2


22. { Become a member
C# Corner Login
23. void Demo1.print()
24. { Post Ask Question
25. Console.WriteLine("Debendra");
26. }
27. void Demo2.print()
28. {
29. Console.WriteLine("Debendra");
30. }
31. static void Main(string[] args)
32. {
33. Program p = new Program();
34. ((Demo2)p).print();
35. Console.ReadLine();
36.
37. }
38. }
39. }

    
Now, I will narrate a real time scenario where we can use interface. I will go for the same
example that I have done earlier.

01. using System;


02. using System.Collections;
03. using System.Collections.Generic;
04.
05. namespace oops1
06. {
07. public class Cars
08. {
09.
10. public string Wheel()
11. {
12. return "4 wheeler";
13.
https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 17/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

14. }
15. public string CheckAC() Become a member
C# Corner Login
16. {
17. return "AC is available"; Post Ask Question
18. }
19. public string CallFacility()
20. {
21. return "Call Facility supported";
22. }
23.
24.
25.
26.
27.
28. }
29.
30.
31. }

Now, here is my Toyota class which is derived from Cars.

01. using System;


02. using System.Collections.Generic;
03. using System.Linq;
04. using System.Text;
05. using System.Threading.Tasks;
06.
07. namespace oops1
08. {
09. public class Toyota : Cars
10. {
11.
12.
13. static void Main(string[] args)
14. {
15.
16.
17. Toyota Toy = new Toyota();
18.
19.
20.
21. Console.WriteLine(Toy.CallFacility());
22. Console.WriteLine(Toy.Wheel());
23. Console.WriteLine(Toy.CheckAC());
24.
25.
26.
27. Console.ReadLine();
28.
29.
30. }
31. }
32.
33.
https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 18/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

34. }
Become a member
C# Corner Login
Here is my Hyundai class which is derived from Cars.
Post Ask Question
01. using oops1;
02. using System;
03. using System.Collections.Generic;
04. using System.Linq;
05. using System.Text;
06. using System.Threading.Tasks;
07.
08. namespace oops1
09. {
10. public class Hyundai:Cars
11. {
12.
13.
14. static void Main(string[] args)
15. {
16. Hyundai dust = new Hyundai();
17.
18. Console.WriteLine(dust.CallFacility());
19. Console.WriteLine(dust.Wheel());
20. Console.WriteLine(dust.CheckAC());
21.
22.
23. Console.ReadLine();
24.
25.
26. }
27. }
28.
29.
30. }

A new feature for the Hyundai car is Introduced called GPS which is not supported in Toyota
cars. Then, how can we implement and which way we can implement these?

Here, we have certain options like

Go for a new class de ning the GPS method and inherit it to the Hyundai Class.
Go for an abstract class and de ne GPS method and inherit it on Hyundai class and
implement the GPS method there.
Directly create a method in Hyundai class and consume it.
Go for Interface
.
CASE 1 - By Using simple class

Let's nd what will happen if we use a class there, and declare a method as GPS and try to

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 19/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

inherit in Hyundai class.


Become a member
C# Corner Login
Created a new class as "NewFeatures, as shown below.
Post Ask Question
01. class NewFeatures
02. {
03. public void GPS()
04. {
05. Console.WriteLine("GPS supported");
06. }
07. }

Now, inherit the Hyundai class from NewFeatures. So, if we check this class, it was
previously inherited from Cars class and for now, again inherits from NefFeatures class.
So, it gives the following error when you try to run the program.

01. using oops1;


02. using System;
03. using System.Collections.Generic;
04. using System.Linq;
05. using System.Text;
06. using System.Threading.Tasks;
07.
08. namespace oops1
09. {
10. public class Hyundai:Cars,NewFeatures
11. {
12.
13.
14. static void Main(string[] args)
15. {
16. Hyundai hun = new Hyundai();
17.
18. Console.WriteLine(hun.CallFacility());
19. Console.WriteLine(hun.Wheel());
20. Console.WriteLine(hun.CheckAC());
21.
22.
23. Console.ReadLine();
24.
25.
26. }
27. }
28.
29.
30. }

Now, run the program and nd out the error.

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 20/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

Become a member
C# Corner Login

Post Ask Question

This is simple because C# does not support multiple inheritance.

CASE 2 - By using Abstract class

Now, go for abstract class and see what happens.

01. public abstract class NewFeatures


02. {
03. abstract public void GPS();
04.
05. }

Now, let's try to inherit from abstract class.

01. using oops1;


02. using System;
03. using System.Collections.Generic;
04. using System.Linq;
05. using System.Text;
06. using System.Threading.Tasks;
07.
08. namespace oops1
09. {
10. public class Hyundai:Cars,NewFeatures
11. {
12. public override void GPS()
13. {
14. Console.WriteLine("GPS supported.");
15.
16. }
17.
18.
19. static void Main(string[] args)
20. {
21. Hyundai hun = new Hyundai();
22.
23. Console.WriteLine(hun.CallFacility());
24. Console.WriteLine(hun.Wheel());
25. Console.WriteLine(hun.CheckAC());
26.
27.
28. Console.ReadLine();
29.
30.
https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 21/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

31. }
32. } Become a member
C# Corner Login
33.
34. Post Ask Question
35. }

So, here is the error I got.

CASE 3 - Direct creating a method called GPS() inside Hyundai class

This is very relevant way to use a unique method but it has a small problem. If for any
reason we forget to create such common method, then it will not ask to write
methods.Today, we are using one unique method so that is OK we can remember and
write the method. Suppose, we have hundreds of such common methods and we forget
to  write 2 of them then it will run but not give the expected output,so we have to skip the
way and go for the fourth one by de ning interface.

CASE 4 - By de ning Interface

This is the most important case and we will see how it will solve our problem.Lets de ne
 the  method in an Interface

01. interface INewFeatures


02. {
03. void GPS();
04.
05. }

Now inherit the Interface from Cars and see without implementing the GPS() method.

01. using oops1;


02. using System;
03. using System.Collections.Generic;
04. using System.Linq;
05. using System.Text;
06. using System.Threading.Tasks;
07.
https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 22/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

08. namespace oops1


09. { Become a member
C# Corner Login
10. public class Hyundai:Cars, INewFeatures
11. { Post Ask Question
12.
13.
14. static void Main(string[] args)
15. {
16. Hyundai hun = new Hyundai();
17.
18. Console.WriteLine(hun.CallFacility());
19. Console.WriteLine(hun.Wheel());
20. Console.WriteLine(hun.CheckAC());
21.
22.
23. Console.ReadLine();
24.
25.
26. }
27. }
28.
29.
30. }

As we have not implemented the method it will show the following error as follow.

So, the problems which happen in case 3 can be easily solved by using Interface. Now,
let's implement the method and see will it solve the problem which arises in Case1 and
case2 as follows.

01. using oops1;


02. using System;
03. using System.Collections.Generic;
04. using System.Linq;
05. using System.Text;
06. using System.Threading.Tasks;
07.
08. namespace oops1
09. {
10. public class Hyundai:Cars,INewFeatures
11. {
12. public void GPS()
13. {
14. Console.WriteLine("GPS supported.");
15.
16. }
https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 23/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

17.
18. Become a member
C# Corner Login
19. static void Main(string[] args)
20. { Post Ask Question
21. Hyundai hun = new Hyundai();
22.
23. Console.WriteLine(hun.CallFacility());
24. Console.WriteLine(hun.Wheel());
25. Console.WriteLine(hun.CheckAC());
26. hun.GPS();
27.
28.
29. Console.ReadLine();
30.
31.
32. }
33. }
34.
35.
36. }

Thus, it resolves the demerits of all the above 3 cases. So, in this situation, we have to use

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 24/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

Interface. Here, I have shown you the sketch diagram.


Become a member
C# Corner Login

Post Ask Question

In this way, we can use abstract class and interface in our project depending on the condition.

Hope this article gives you an idea about when to use these 2 important things.If you have any
suggestions, please feel free to comment so that I can include that in this article.

AbstractClass Interface Interface Vs Abstract Class

Next Recommended Article


C# Interface Based Development

OUR BOOKS

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 25/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

Become a member
C# Corner Login

Post Ask Question

Debendra Dash

INDIAN| PROGRAMMER| BLOGGER


https://www.c-sharpcorner.com/members/debendra-dash

159 6.3m 3

View Previous Comments


109 99

Type your comment here and press Enter Key (Minimum 10 characters)

Is it recommended to use abstract and interface at the same time? I'm facing a situation in my APIs
where Toyota and Hyundai implementing some common features di erently while having some
INewFeatures of themselves. Using interface will do the job, but, I would like to enforce more rules
by using abstract so that people will always follow the standard naming of those common
features. However, this is creating a problem where the abstract function and interface are having
redundant signature and new common features will likely to be added to the interface but not the
abstract
Weng Kin Chan Nov 14, 2020
1973 11 0 0 0 Reply

Thank u so much Sir.


Puja Tundalwar Sep 28, 2020
1924 60 0 0 0 Reply

It was a nice Explanation given by you. It helps a lot to understand when to use abstract and
interface
Manikanta Nani Aug 15, 2020
1967 17 0 0 0 Reply

Excellent explanation for abstract but interface good(its ok) expected more explanation for
advantages of interface
Thirumalarao Thotapalli May 30, 2020
1927 57 0 0 0 Reply

Awesome Explanation of both Abstract class and Interface. Thanks a lot for this.
Vijay Kale May 20, 2020
1935 49 0 0 0 Reply

It is very useful to me. Thanks for you excellent explanation.


mari arun Feb 25, 2020

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 26/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

1890 94 0 0 0 Reply
Become a member
C# Corner Login

Post Ask Question


Nice Article. Thanks
Dinesh Gabhane Nov 12, 2019
100 20.8k 452.4k 2 0 Reply

Excellent explanation
alibasha syed Oct 16, 2019
1900 84 29.8k 1 0 Reply

We can create an object of abstract class and interface but we can not create instance of both.
Please con rm what is the di erence between object and instance of a class?
vishal yadav Sep 06, 2019
1854 130 0 3 1 Reply

By creating object of abstract class or inheritance, we still referring child class means we
still initialize child class. Class initialize means do memory allocation for this class using
new keyword.
kunal Sheth Sep 10, 2019
1938 46 0 2

Awesome beautifully explained.


Sathiya Moorthy Aug 20, 2019
1849 135 0 3 0 Reply

FEATURED ARTICLES

Getting Started Wth MongoDb Atlas

What Is a Full Stack Developer

C# 9 Cheat Sheet

💠 Clean Architecture End To End In .NET 5

Flutter Vs React Native Which is Best


View All

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 27/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

Become a member
C# Corner Login

Post Ask Question

TRENDING UP

01 Angular 11 CURD Application Using Web API With Material Design

02 Basic Authentication in Swagger (Open API) .Net 5

03 Prototype Design Pattern With Java

04 Background Tasks Made Easy With Hang re And .Net 5

05 Service Worker – Why required and how to implement it in Angular Project?

06 Encapsulation In Java

07 How To Calculate The Sum Of A Table Column In Angular 10

08 How To integrate Dependency Injection In Azure Functions

09 How To Integrate Application Insights Into Azure Functions

10 Six Types Of Regression | Detailed Explanation


View All

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 28/29
1/28/2021 When To Use Abstract Class and Interface In Real Projects

Become a member
C# Corner Login

Post Ask Question

About Us Contact Us Privacy Policy Terms Media Kit Sitemap Report a Bug FAQ Partners

C# Tutorials Common Interview Questions Stories Consultants Ideas Certi cations


©2021 C# Corner. All contents are copyright of their authors.

https://www.c-sharpcorner.com/article/when-to-use-abstract-class-and-interface-in-real-time-projects/ 29/29

You might also like