Definition of an Interface?
An interface provides a specification rather than an
implementation for its members. The members of interface will be implemented by
the classes and structs that implement the interface. An interface can contain
only methods, properties, events, and indexers (an abstract class also precisely contains the same
members). An interface is special in the following ways:
- Interface members are all implicitly abstract. In contrast, a class can provide both abstract members and concrete members with implementations.
- A class (or struct) can implement multiple interfaces. In contrast, a class can inherit from only a single class, and a struct cannot inherit at all.
Interface members are always implicitly
public and cannot declare
an access modifier. Implementing an interface means providing a public
implementation for all its members:
If a class
that implements an interface does not define all the methods of the interface,
then it must be declared
abstract and the method definitions must be provided by the subclass that
extends the abstract class. In
addition to this an interfaces can inherit other interfaces.
interface
ISum
{
int iGetSum(int i, int j); //By Default Public
int iGetSum(int i, int j); //By Default Public
}
class Sum : ISum
{
class Sum : ISum
{
public int iGetSum(int i, int j) //Must be declare Public
{
return i + j;
}
}
Definition of an Interface, real world scenarios and live
code example for interface, use of interface in c # sharp dot .Net, Interface members are always implicitly
public and cannot declare
an access modifier