This article is the second part of the series “Abstract Class & Interface: Two Villains of Every Interview” and explains the important key points of interfaces.
Introduction to Interface
In my first article, I received a lot of feedback, some positive, some negative and the neutral but one question everyone asked is why I implied they are villains. I just want to clarify that I did not intend to portray them as villains. These are the most important concepts in programming and without them we cannot get better code efficiency.
Mostly in every interview, the first question is, "What is the difference between an Abstract Class and an Interface" and your entire interview depends on the answer to this question. Remember, “The first impression is the last impression”. So, these concepts are not villains but their role in an interview surely is. Enough about the critics, let's move to the next part of the series: interfaces.
An interface is the most important element of a software project and without it, a project is incomplete. Many wouldn't agree with me and debate that. Yes, it is a debatable question whether or not to use an interface. If yes, then how? In this article, I'm going to explain the important key aspects of interfaces. Like my previous article about abstract classes, I used the same approach of “What, Why and How” in this article and tried to explain it in the simplest possible way.
Roadmap
If you're reading this article for the first time, I request you to go for Part 1 of this series and then return later to this article. Here is the road map of the series of articles under the title “Abstract Class & Interface: Two Villains of Every Interview”:
Interface
What an interface is
In the real world, an interface means a medium to interact with something. To be precise, it's a point where two systems, subjects, organizations and so on meet and interact. There are a few rules for the interaction to be done. Suppose you are going for an interview of Programmer Profile. The interview is only possible if the interviewer and you speak the same language. Moreover, you and the interviewer have the same skill set of programming languages to discuss upon.
Similarly, in the programming world, an interface means a contract to interact with multiple code modules. If a class wants to communicate with an interface, it must implement it and define its members. Consider it like the interviewer's question and you need to answer it correctly, if you want the job.
The MSDN Library defines the interface like a pure abstract class. An interface contains only the signatures of methods, properties, events, or indexers. It has no implementation of its own and can only be implemented by a class or a struct. Any of the two that implement the interface must provide the definitions to members specified in the interface. It is like a contract for all the derived classes to follow.
An interface is declared using the interface keyword. interface members are implicitly public and abstract, so we cannot prefix any access modifiers to it. An interface cannot contain fields, constant members, constructors, destructors and static members.
Why we need an interface
An interface is not a class. It contains only method signatures. It has no implementation on its own and cannot be instantiated. Its implementation logic is provided by the classes that derived from it. An interface is mostly considered to be a pure abstract class. However, there is the advantage of using an interface over an abstract class; that is "Multiple Inheritance Support". In C#, two classes (either abstract or concrete) cannot be inherited by the same derived class. It causes ambiguity in the derived class if both have the same method signature. We can do multiple inheritance in C# using interfaces.
An interface plays a vital role in the Service Oriented Architecture (SOA). In WCF, we use interfaces to define Service Contracts. A single class can implement any number of Service Contract Interfaces. It is generally accepted as the best practice to do so. However, we can also use classes for Service Contracts.
Most of the Design Patterns and Principles are based on interfaces rather than class inheritance. Some of the examples are Builder Design Pattern, Factory Pattern, Interface Segregation Principle and so on.
How to define an interface
Suppose we need to define a class for a Smart Phone. The class can have members like OS, AppStore and Call. The Smartphone can be either Android based or iOS based and cannot be both. There is no common functionality between an Android and iOS Smartphone, so we don't need to provide any default functionality. One approach is to make the SmartPhone class abstract and also all its members abstract. This approach works fine and several concrete classes like Samsung, Apple, HTC can inherit from it.
Now, after a few days Apple wants to add a Touch ID feature to its Smartphone. You can add TouchID as an abstract method in your abstract base class SmartPhone. But what if HTC doesn't want that feature and neither does Samsung? So, the TouchID method cannot be placed inside the abstract class SmartPhone. An alternative is to define another abstract class Features and add the TouchID method to it. This is also a bad idea since C# doesn't support inheritance of multiple classes (abstract or concrete) into a derived class.
In this situation, an interface is useful and plays a vital role in solving the problem. An interface provides only the method definitions, just like an abstract class, but can be useful in multiple inheritances. You can make the Features class an interface and add the TouchID method to it. It provides only the method signature and whichever class inherits it can implement it in its own way. It is also completely valid for a class to inherit more than one interface in C#. Also, we can make the SmartPhone class an interface instead of an abstract class. It is better instead of making a pure abstract class, we can use interfaces.
Note: The example is not a best one, but I think it gets the point across. It is just for the sake of understanding interfaces.
Let us consider the example discussed above and create a Console Application for it. Open Visual Studio and add a new console project as "InterfaceDemo".

By default, it gives a class named Program with a Main method in it for code execution. Let's create an abstract class SmartPhone and define OS and AppStore abstract methods in it. We can create an abstract class by putting the keyword "abstract" before a class definition. If you're not familiar with abstract classes, please go to the Part 1 of this series.
- using System;
- namespace InterfaceDemo
- {
- //Abstract Class SmartPhone with only abstract methods in it
- abstract class SmartPhone
- {
- public abstract void OS();
- public abstract void AppStore();
- }
- class Program
- {
- static void Main(string[] args)
- {
- }
- }
- }
- using System;
- namespace InterfaceDemo
- {
- //Abstract Class SmartPhone with only abstract methods in it
- abstract class SmartPhone
- {
- public abstract void OS();
- public abstract void AppStore();
- }
- class Apple : SmartPhone
- {
- public override void OS()
- {
- //Some Implementation Here
- }
- public override void AppStore()
- {
- //Some Implementation Here
- }
- }
- class Samsung: SmartPhone
- {
- public override void OS()
- {
- //Some Implementation Here
- }
- public override void AppStore()
- {
- //Some Implementation Here
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- }
- }
- }
- using System;
- namespace InterfaceDemo
- {
- //Abstract Class SmartPhone
- abstract class SmartPhone
- {
- public abstract void OS();
- public abstract void AppStore();
- //TouchID method meant only for Apple Class
- public abstract void TouchID();
- }
- class Apple : SmartPhone
- {
- public override void OS()
- {
- //Some Implementation Here
- }
- public override void AppStore()
- {
- //Some Implementation Here
- }
- //Implementing the TouchID feature
- public override void TouchID()
- {
- //Some Implementation Here
- }
- }
- class Samsung: SmartPhone
- {
- public override void OS()
- {
- //Some Implementation Here
- }
- public override void AppStore()
- {
- //Some Implementation Here
- }
- }
- class Program
- {
- static void Main(string[] args) { }
- }
- }

It throws an error saying that the Samsung class doesn't implement the TouchID method. By the definition of abstract class, any class implements it must provide definitions to all its abstract members. The TouchID method is meant only for the Apple class and the Samsung class doesn't want to implement it. It clearly seems that our approach is wrong since the TouchID method cannot be placed in the SmartPhone abstract class.
An alternative approach is to define another abstract class Features and define the TouchID method to it. This approach seems fine since whatever class inherits Features can implement the TouchID method.
- using System;
- namespace InterfaceDemo
- {
- //Abstract Class SmartPhone
- abstract class SmartPhone
- {
- public abstract void OS();
- public abstract void AppStore();
- }
- //Abstract Class Features for TouchID method
- abstract class Features
- {
- public abstract void TouchID();
- }
- //Apple Class inherits both SmartPhone and Features
- class Apple : SmartPhone, Features
- {
- public override void OS()
- {
- //Some Implementation Here
- }
- public override void AppStore()
- {
- //Some Implementation Here
- }
- //Implementation of TouchID method in Apple Class
- public override void TouchID()
- {
- //Some Implementation Here
- }
- }
- class Samsung : SmartPhone
- {
- public override void OS()
- {
- //Some Implementation Here
- }
- public override void AppStore()
- {
- //Some Implementation Here
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- }
- }
- }
Let's compile the code and see what happens.

It again throws an error saying we cannot have multiple base classes in a derived class. This is called Multiple Inheritance of classes and is not allowed in C#. So, our second approach also fails to implement the TouchID method. This is where an interface is useful and helps to solve the "Multiple Inheritance" issue in C#. We can define both the SmartPhone and Features as interfaces and let the classes implement them as they need to. We can also have more than one interface in a class. This is the only way to do multiple inheritance in C#.
Let's re-create the same project using interfaces. We can create an interface using the keyword interface. It is considered a good practice to prefix "I" before the interface name, however the point is arguable and the choice is yours.
- using System;
- namespace InterfaceDemo
- {
- interface ISmartPhone //Definition of Interface
- {
- public void OS();
- public void AppStore();
- }
- class Program
- {
- static void Main(string[] args)
- {
- }
- }
- }
We have defined the interface ISmartPhone with the method signatures OS and AppStore in it. If we compile the code now, it throws an error straightaway.

It says we cannot prefix public modifiers with method signatures. In fact, no access modifier is allowed with interface methods. Interface methods are implicitly public in C# because an interface is a contract meant to be used by other classes. Moreover, we must declare these methods as public in derived classes, when we provide implementations to these methods. Also, we cannot declare these methods as static.
- using System;
- namespace InterfaceDemo
- {
- interface ISmartPhone //Definition of Interface
- {
- static void OS();
- static void AppStore();
- }
- class Program
- {
- static void Main(string[] args)
- {
- }
- }
- }

Let's define the interface methods without any access modifier and create a concrete class Apple that inherits the ISmartPhone interface and provides definitions to its members.
- using System;
- namespace InterfaceDemo
- {
- interface ISmartPhone
- {
- void OS();
- void AppStore();
- }
- class Apple: ISmartPhone
- {
- //OS Method Implementation
- public void OS()
- {
- Console.WriteLine("OS Method: The OS of this Smartphone is iOS8");
- }
- //AppStore Method Implementation
- public void AppStore()
- {
- Console.WriteLine("AppStore Method: The Application Store of this Smartphone is iTunes");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- }
- }
- }






KishPosted Feb 7, 2019, 12:28 AM
TouchID- In part 1 you mentioned we can define local methods in derived class. so we can add TouchID method only for Apple class. Then no need of interface here...
vijayakumar JothiPosted Feb 8, 2018, 4:10 AM
TouchID - can be implemented as virtual method, and implement by override only in Apple class, leave as it is, in other classes like Samsung - then no need of interface here.
manoj singhPosted Jan 25, 2018, 4:50 AM
One interviewer asked me about Why c# does not support multiple inheritance Then i Explain the ambiguity thing happens in multiple inheritance. Then he say pure abstract class looks like interface then why we can not inherit more then one pure abstract class
Vijendra KushwahaPosted Jan 12, 2018, 8:12 AM
Great article , thanks for sharing !!
Pardha GoudPosted Jan 6, 2018, 12:13 AM
Thanq very much Sir!
Abhinash DoraPosted Oct 27, 2017, 8:37 AM
Very well explained in both part 1 and part 2. It removes all my doubts about this. Now the two villains will become my friends. :)
Zeeshan SattarPosted Sep 12, 2017, 1:23 PM
Hats off amazing explanation :) (Y)
Maddy NPosted Jul 27, 2017, 11:45 PM
First Of all article is well explained. I have a query. Suppose, In future, we need to introduce one more type of phone in which we have not the concept of AppStore(); then in this case what will you suggest?
Nghia PhanPosted Jul 26, 2017, 12:51 AM
It is very useful. Thanks you so much!
Madhuri VPosted Jul 17, 2017, 10:10 AM
Really your explanation is simply cleared my confusion.....Thanks alot Sahil:)
Mazhar PashaPosted May 26, 2017, 6:51 AM
Awesome..Explained in detail..If anybody gone through these 2 articles then their confusion will go. Thanks for sharing..
vijay patelPosted Jan 21, 2017, 7:07 AM
Please reply on Harminder Singh comment.. i am also waiting for your response.
Ravi KumarPosted Dec 14, 2016, 8:29 AM
Good one, Sahil and and well explained.
mayank GuptaPosted Nov 25, 2016, 1:50 AM
I think this article should get the award for Best explanation about Interface & Abstraction. Even MSDN is far behind it. Extraordinary write. Thank You for your efforts ! .
Soumalya DasPosted Aug 19, 2016, 2:41 AM
Best airtical i ever seen in the net
SatishPosted Jul 27, 2016, 3:23 AM
Ever best article , I have read so far.. thank you
Bhuvanesh MohankumarPosted May 10, 2016, 2:30 AM
Very nice article :)
Sabyasachi MishraPosted Mar 16, 2016, 6:19 AM
Good one and well explained.
Anil Kumar MurmuPosted Mar 14, 2016, 5:16 AM
good one
Ajeet MishraPosted Feb 27, 2016, 4:31 AM
Really nice
Jaipal ReddyPosted Jan 12, 2016, 12:30 AM
Good article. .
Harminder SinghPosted Nov 6, 2015, 2:42 AM
good article and nice explanation. Sahil Can't we add a local touchid() method to the apple class and use it( in case we don't want interface).
Sahil SharmaPosted Oct 15, 2015, 1:54 AM
Thanks a lot guys for such lovely comments. I'm glad to know it helps you to understand the different aspects of Abstract Class and Interface.
Hemlata KushwahaPosted Oct 15, 2015, 1:03 AM
Best article for me. Superb Explanation. I was searching the difference between Abstract and interface from Long time. Now finally i got it satisfied Answer from your Article. Nice!
David SmithPosted Oct 8, 2015, 8:04 AM
The best article of all time. Thank God someone finally explain the difference from the start to end. I completely understand. Do you have and example explaining the difference between delegates and events just as you did akbstract class vs interface.
Upendra Pratap ShahiPosted Sep 10, 2015, 6:44 AM
superb sir....very nice explaination
Anuj JainPosted Jul 11, 2015, 1:25 PM
Presented each concepts very nicely. Great Job!
SharadPosted Jun 27, 2015, 12:56 AM
nice blog with good example...
Anil KumarPosted Jun 16, 2015, 3:19 AM
Thank you,Enjoyed Taste of abstract vs interface (Spoon feeding).Keep Move on...........
Sahil SharmaPosted Jun 3, 2015, 11:42 AM
Thank you all for such comments. I'm glad you like this article.
Kamal SharmaPosted Jun 3, 2015, 9:54 AM
This is one of best article on abstract VS interface, I have seen on blogs Great work
Vinod TGPosted May 3, 2015, 2:38 AM
Awesome.. Cleared many confusions :)
Atieq ur RehmanPosted Apr 15, 2015, 4:33 PM
you said in we can not make instance of an abstract class in part 1 and in part 2 you said that interface is a pure abstract class then why you make instance of IDebitCard and ICreditCard ??? in main ?? IDebitCard DebitCard = new Customer(); ICreditCard CreditCard = new Customer();
NitinPosted Mar 11, 2015, 5:31 AM
great work..very informative.
Ritesh PatelPosted Mar 9, 2015, 7:08 AM
Excellent tutorial.
Ritesh PatelPosted Mar 9, 2015, 7:07 AM
Hello
Khargesh RajputPosted Mar 9, 2015, 1:13 AM
thanks for sharing important content
Casper BarchagerPosted Mar 8, 2015, 4:59 PM
Very good and easy to understand. Good explanation of why to use an interface instead of an abstract class in this example.
Rahul SinghPosted Dec 17, 2014, 12:25 AM
Nice
Deb Kumar GhosalPosted Oct 20, 2014, 7:27 AM
In one of the Interview I was asked "It is possible for a class A to implement only two methods out of four methods of InterFace "IInterface"? and class B will implement rest two methods inside the class? Is it possible, or allowed?
JUKE BOXPosted Oct 17, 2014, 6:14 AM
but in ur article u define interface as a pure abstract class supporting multiple inheritance???
JUKE BOXPosted Oct 16, 2014, 4:45 PM
since we can use constructors in abstract class ..why cant we use it in interface?????
Udaya kumarPosted Oct 13, 2014, 1:33 AM
very super man.
Siva Rama Krishna KrapaPosted Oct 8, 2014, 7:20 PM
Great article..Keep going
Gursewak SinghPosted Sep 26, 2014, 3:35 AM
Nice Article.
priya PerumpilavilPosted Sep 24, 2014, 9:02 AM
thanks Sahil Sharma for the post. It is very simple and understandable..good job
Sandeep VemulaPosted Sep 24, 2014, 8:43 AM
good explanation with example....
salman alyPosted Sep 24, 2014, 2:22 AM
Great Post , your way of explanation is simply awesum
Rajeev RanjanPosted Sep 24, 2014, 12:43 AM
Dear sahil :- your writing skill is superv, also your example makes a great impact on mind. Such a great example you took.
Jai SinghPosted Sep 23, 2014, 4:34 PM
Nice post
Manju lata YadavPosted Sep 23, 2014, 11:02 AM
Sahil whenever u got time Edit :-Interface Reference Variabl in keypoint section.