Interface
interfaces in C# doesn't support multiple inheritances in C#, meaning you cannot inherit from multiple classes but you can implement multiple interfaces.
Note - Multiple inheritance is form of inheritance where two or more classes are inherited by the derived class is called multiple Inheritance .NET framework of removing Multiple inheritance support. because Diamond Shape Problem occur . then coming to picture Interface .
Interface Introduction :
In C#, Interface is a keyword, which holds a group of abstract methods and properties, which are used by an abstract or non-abstract class. Defining the methods are properties inside an interface which makes them public access.
Example : Suppose you have developed an application which inserts, updates, deletes and searches something into the database. for example "Student information system" for your college portal. Some of the activities like capturing student credentials (Insert), searching a student profile (Search) etc. Similarly suppose you have another module where you are capturing fee details for students (Insert), modifying fee details for a student(update)etc. So for each case you are inserting, updating or searching. so come into the picture an interface where where we can declare all method related to insert/update/search . we can use what ever you need . so you can also achieve loosely couple.
Loosely coupling – we have classes which communicate with each other.
When to use Interface?
- Security: When we have to simply hide some features and have to use those later. It is essential to hide a few details while only showing the details important to the user.
- Multiple Inheritance: Multiple Inheritance is not supported in C# . But the use of an interface, multiple interfaces can be implemented into a single class.
Interface Advantages
- One of the major advantages of Interface implement multiple inheritances.
- Complete Abstraction can be achieved by the implementation of Interface.
- Along with making our code easy to maintain, concept loose coupling can be achieved.
Important Point :
1 . interface cannot define any constructor methods and neither they can define any instance fields and also it cannot contains any static members.
2. You cannot apply access modifiers to interface members. All the members are public by default. If you use an access modifier in an interface, then the C# compiler will give a compile-time error "The modifier 'public/private/protected' is not valid for this item."
3. An interface can only contain declarations but not implementations. The following will give a compile-time error.
Interview Ask you frequently question .
Interface I1
{
int prop
{
get;
set;
}
IDisposable, IEnumerable, ICollection
Can an interface inherit from another interface?
Yes, an interface can inherit from another interface. It is possible for a class to inherit an interface multiple times, through base classes or interfaces it inherits. In this case, the class can only implement the interface one time. the class inheriting the interface can change the interface behavior by overriding the virtual members.
Can an Interface contain fields?
No, an Interface cannot contain fields
What do you mean by “Explicitly Implementing an Interface”? Give an example?
If a class is implementing the inherited interface member by prefixing the name of the interface, then the class is “Explicitly Implementing an Interface member”.