In this article, I will discuss the concept of Interface in C#. Please read our last article before proceeding to this article. In our last article, we discussed inheritance in C#.
We know the concept of multiple inheritances where one class is derived from more than one superclass. For example a definition likes
class A: B, C
{
}
But this concept is not supported by .NET with classes.
Since a large no of real-time application required the use of multiple inheritances, where we inherit properties and behaviors from several different classes.
That’s why .NET provides an alternative approach known as the interface to support the concept of multiple inheritances.
What is an interface?
The interface is a fully un-implemented class used for declaring a set of operations of an object. So, we can define an interface as a pure abstract class which allows us to define only abstract methods. Abstract method means a method without body or implementation.
What is the need of interface when we have the abstract class to define abstract methods?
.NET doesn’t support multiple inheritances with classes. So we must use interface as the superclass to develop abstraction for supporting multiple inheritances. If we define an abstract class in place of an interface, a service provider cannot implement multiple specifications so that the service provider cannot have multiple businesses.
What are the different types of inheritance?
A class can be inherited either from another class or from an interface also. So inheritance can be divided into two categories
- Implementation inheritance
- Interface inheritance
If a class is inheriting from another class we call it as implementation inheritance and the main concept of implementation inheritance is child classes can consume the members of its parent class.
On the other hand, if a class is inheriting from an interface we call it as interface inheritance but interface inheritance does not provide any reusability because here we are not consuming the members of the parent under the child. The child is only implementing the parent’s members.
How to declare an interface?
By using the keyword interface we can declare an interface.
Here the keyword interface tells that Example is an interface containing one abstract method such as show().
By default the members of an interface are public. An interface can contain
- Abstract methods
- Properties
- Indexes
- Events
An interface cannot contain
- Non-abstract functions
- Data fields
- Constructors
- Destructors
Should I use public access modifiers for interface methods?
.NET interface methods are implicitly public by default, even if they belong to nested interfaces. Non-public modifiers are not valid or necessary for interface methods. So the compiler will fail and warn you in this case. Nested interfaces may be declared protected or private but not the interface methods.
Can an interface implement an abstract class?
No. In .NET an interface cannot implement an abstract class. An interface may only extend a super interface. However, an abstract class can implement an interface because an abstract class can contain both abstract methods and concrete methods.
Can an interface be declared as sealed?
No, it is not permitted to declare an interface as sealed; it will cause a compilation error. This is a .NET language design decision. Interface types are intended to be implemented and can be extended without restriction.
Is more than one interfaces are allowed to implement a class?
Yes, a class can implement multiple interfaces; this is an effective way to achieve multiple inheritances in .NET. But a class can extend only one superclass.
Is it necessary to implement all interface methods?
It is not necessary for a class that implements an interface to implement all its methods, but in this case, the class must be declared as abstract.
How interface is different from a class?
An interface is different from a class in the following ways:
- We cannot instantiate an interface.
- An interface does not contain any constructor or data fields or destructor, etc.
- All of the methods of an interface are abstract and public by default.
- An interface is not extended by a class; it is implemented by a class.
- An interface can extend multiple interfaces.
What are the similarities between the interface and abstract class?
An interface is similar to an abstract class in the following ways
- Both interface and the abstract class cannot be instantiated means we cannot create the object.
- But we can create a reference variable for both interface and abstract class.
- The subclass should implement all abstract methods.
- Both cannot be declared as sealed.
What is the main difference between interface and abstract class?
The main difference is to be answered in the interview is as follows
The interface is a fully un-implemented class used for declaring a set of operations of an object. The abstract class is a partially implemented class. It implements some of the operations of an object. These implemented operations are common for all next level subclasses. Remaining operations are implemented by the next level subclasses according to their requirement.
The interface allows us to develop multiple inheritances. So we must start object design with interface whereas abstract class does not support multiple inheritances so it always comes next to interface in object creation process.
What are the differences between the interface and abstract class?
Abstract class | Interface |
It is a partially implemented class. It allows us to define both concrete and abstract methods. | It is a fully un-implemented class. It allows us to define only abstract methods. |
It provides both reusability and forcibility | It provides the only forcibility |
It should be declared as abstract by using the abstract keyword, abstract methods should also contain the abstract keyword. | It should be created by using the keyword interface. Declaring its methods as abstract is optional because by default the methods of an interface are abstract. The compiler places abstract keyword at the time of program compilation. |
A class which contains one or more abstract function is called an abstract class. | The class which contains all the abstract functions is known as an interface. |
Its members default accessibility modifier is private and can be changed to any of the other accessibility modifiers. | Its members default accessibility modifier is public and cannot be changed. |
It is possible to declare data fields in an abstract class. | But it is not possible to declare any data fields in an interface. |
An abstract class can contain the non-abstract function. | An interface cannot contain non-abstract functions. |
An abstract class can inherit from another abstract class or from an interface. | An interface can inherit from only other interfaces but cannot inherits from the abstract class. |
It can have inner classes | It can also have inner classes. |
An abstract class cannot be used to implement multiple inheritance. | An interface can be used to implement multiple inheritance. |
Abstract class members can have access modifiers. | Interface members cannot have access modifiers. |
Rules to follow while working with the interface
While working with an interface, we must follow the below rules.
- The interface cannot have concrete methods, violation leads to CE: interface methods cannot have a body.
- We cannot declare interface members as private or protected members violation leads to CE:” modifier is not allowed here”.
- An interface cannot be instantiated but its reference variable can be created for storing its subclass object reference.
- We cannot declare the interface as sealed it leads to CE: “illegal combination of modifier interface and final”.
- The class derived from interface should implement all abstract methods of interface otherwise it should be declared as abstract else it leads to compile time error.
- The subclass should implement the interface method with public keyword because interface methods default accessibility modifier is public.
- In an interface, we cannot create fields variable violation leads to compile time error.
We have discussed a lot of theory. Let’s move towards example.
Let’s us see a simple example for better understanding.
OUTPUT:

Example2: WAP TO CALCULATE AREA OF RECTANGLE, CIRCLE USING INTERFACE.
OUTPUT:

In the next article, I will discuss multiple inheritance using interface in C#.
Please refer below MSDN article for more information.
SUMMARY:
In this article, I try to explain Interface in C# step by step with some simple examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.