What is an interface? Interface literally means a medium for working between two things, such as a user interface. When we say user interface, it is the interface between the user and the system, allowing user to interact with the system.
In Object Oriented Programming (OOP), interface is a kind of contract between Interface type and the Class type. Interface is a type like class but interface can contain only methods, properties, events and indexers as its members. Unlike class, interface does not have any field because interface does not deal with state but with behavior. The methods in the interface are always implicitly public and abstract. Interface represents abstract behaviors which are implemented by the contracting class. The contracting class is the class that implements the interface.
Definition
Interface is a type like class that contains abstract behaviors represented by methods, properties, events and indexers as its members; it does not contain states of objects which are represented by fields. All the members of an interface are implicitly abstract and public but cannot be explicitly declared them abstract and public; doing so will throw compile time error.
Interface Members
So, following important facts should be remebered when working with members of interface:
- Interface deals with abstract behavior, not with state of objects. Therefore, fields cannot be defined inside an interface. We can declare objects behavior such as methods, events, properties and indexers.
- We cannot define methods, events, properties and indexers inside interface. We can only declare them inside interface because interface deals with abstract behavior.
- We cannot use public and abstract keyword with methods, events, properties and indexers in interface. The methods, events, properties and indexers are implicitly public and abstract in interface.
- It means that properties in an interface cannot have get and set accessor with {} after get and set keywords. Example
interface Interface1 {int Property1 { get; set; }}
- It means that indexer in an interface cannot have get and set accessor with {} after get and set keywords. Example
interface Interface1{int this [int Index] { get; set; }}
- It means that methods in an interface cannot have {}, after () which follows after method identifier. Example
interface Interface1{int Method1();}
In C#, we know that although a class can inherit another class, a class cannot inherit more than one class. In other words, C# does not support multiple inheritance. But C# allows to inherit multiple interfaces by a single class instead of inheritance of multiple classes.
Although a class can inherit only one other class, yet a class can inherit one or more interfaces.
A class can inherit another class and multiple interfaces at the same time.
Class vs Interface
- A class can have field but an interface cannot have field.
- A class defines states and behavior but interface deals with behavior only.
- A class can have abstract , non abstract or any other kind of methods/members but interface has all its methods/members abstract and public methods.
- Interface provides contract and class implements that contract.
- Interface methods are abstract but 'abstract' modifier are not used with them but a class with abstract method must have abstract modifier with the method and the class.
- Instance of a non abstract class can be created but instance of interface can never be created. (Because the methods of interface are always abstract.)
- A class can inherit an interface but not vice versa.
- When a class inherits an interface, it cannot consume the interface directly. The class must, first of all, implement all the methods of the interface, only then the class can consume the methods of the interface.
- But when a class inherits another class, it is not necessarily required to implement the methods of the parent class. If the parent class has virtual methods, they can or cannot be overridden by the child class. The virtual implies that the method is override-able in the child class. The child class can consume the parent class's methods directly and override them if needed. A parent class method without virtual modifier cannot be overridden by the child class.
Class Inheritance Vs. Interface Implementation
- So, when a class inherits another class it can consume the methods of its parent class directly or by overriding the methods, in other words, it can be re-implemented but in case of interface whenever a class inherits an interface, it must always implement all the methods of the interface because the methods within the interface are abstract.
- Inheriting class cannot consume the interface methods unless and until all methods of the interface are implemented by the consuming class.
- Methods do not have any implementation within the interface; they are abstract. So whenever the class inherits an interface, it must implement the methods of the interface. Implementation of all the methods of interface by the inheriting class is mandatory. For example, to consume the InterfaceEx interface, the AddClass class implements all the methods of the InterfaceEx interface.
namespace InterfaceEx
{
interface MyInterface
{
int Calculate(int num1, int num2);
void DisplayResult(int result);
}
}
using System;
namespace InterfaceEx
{
class AddClass : MyInterface
{
public int Calculate(int num1, int num2)
{
return num1 + num2;
}
public void DisplayResult(int result)
{
Console.WriteLine("Sum of two integers: {0}", result.ToString());
}
}
}
The Program class is a client class that eventually consumes the AddClass class.
using System;
namespace InterfaceEx
{
class Program
{
static void Main(string[] args)
{
AddClass c1 = new AddClass();
c1.DisplayResult(c1.Calculate(20, 40));
Console.ReadLine();
}
}
}
Some Facts about Interface
- When a class inherits an interface, it must implement all the methods of the interface, it cannot consume the interface without implementing its all methods.
- All the methods of interface are public and abstract but the explicit use of public and abstract keywords during method declaration is prohibited with the methods of interface.
- There is no field in an interface because interface does not deal with states of object.
- Methods, Properties, Events and Indexers are allowed as members of Interface which represent as behavior of objects of implementing class.
- The members of interface - Methods, Properties, Events and Indexers - are implicitly public and abstract. But we cannot use public or abstract keyword with interface members; doing so will throw error by compiler.
- Only public or internal access modifiers can be used with a class. Similarly, public or internal access modifiers can be used with interfaces.
- One interface can inherit another interface. It means that methods of parent Interface will be inherited by the child Interface. Two interfaces can have methods of the same name with the same method signature. When a class implements both these interfaces then it has to adopt the methods.
- The latest C# version allows to implement methods inside interface but all new development is skipped in this post.

No comments:
Post a Comment