Interface is a contract that defines the signature of the functionality. It looks like a class but has no implementation. It has only empty definition of methods, functions, events, and indexer.
Interfaces provide forced implementation. For instance in the below code snippet we have created a simple interface called as “IDbCompulsory”. The below classes who implement interface “IDbCompulsory” has to provide implementation for “ExecSql”.
interface IDbCompulsory
{
void ExecSql();
}
public class SQLServer :
IDbCompulsory
{
public void ExecSql()
{
// Here code for firing SQL Server SQL statements
// are written
}
}
public class Oracle :
IDbCompulsory
{
public void ExecSql()
{
// Here code for firing Oracle SQL statements
// are written
}
}
Do interface have accessibility modifier?
All elements in Interface should be public. So no accessibility modifier is required.
Can we create an object of abstract class or an interface?
No.
Also see following .NET OOPS
interview questions video on implementing interfaces with same method names
in C#: -