A interface is also a user-defined type. Interface can have methods, properties, events, and indexers as its members. But interfaces will contain only the declaration of the members. Interface contains only Abstract Methods (Methods without method body)
Every abstract method of an interface should be implemented by the child class of the interface without fail (Mandatory).
Note: A class can inherit from a class and interface at a time.
Note: We can't declare any fields/variables under an interface.
The default scope of the member of an interface is public whereas its private in case of a class. By default every member of an interface is abstract so we don't require to use abstract modifier again just like we do in case of abstract class.
Code Example:
interface ITestInterface1
{
void add(int a,int b);
}
interface ITestInterface2 : ITestInterface1
{
void sub(int a, int b);
}
class ImplementationClass : ITestInterface2
{
public void sub(int a, int b) //public modifier is mandatory
{
Console.WriteLine(a - b);
}
public void add(int a, int b)
{
Console.WriteLine(a + b);
}
static void Main(string[] args)
{
ImplementationClass obj = new ImplementationClass();
obj.add(10, 20);
obj.sub(20, 10);
//Or we can create an parent instance using child reference
ITestInterface2 i = obj; //i is an parent instance creating using child "obj" reference
i.add(30, 40);
i.sub(30, 10);
Console.ReadLine();
}
}
Every abstract method of an interface should be implemented by the child class of the interface without fail (Mandatory).
Note: A class can inherit from a class and interface at a time.
Note: We can't declare any fields/variables under an interface.
The default scope of the member of an interface is public whereas its private in case of a class. By default every member of an interface is abstract so we don't require to use abstract modifier again just like we do in case of abstract class.
Code Example:
interface ITestInterface1
{
void add(int a,int b);
}
interface ITestInterface2 : ITestInterface1
{
void sub(int a, int b);
}
class ImplementationClass : ITestInterface2
{
public void sub(int a, int b) //public modifier is mandatory
{
Console.WriteLine(a - b);
}
public void add(int a, int b)
{
Console.WriteLine(a + b);
}
static void Main(string[] args)
{
ImplementationClass obj = new ImplementationClass();
obj.add(10, 20);
obj.sub(20, 10);
//Or we can create an parent instance using child reference
ITestInterface2 i = obj; //i is an parent instance creating using child "obj" reference
i.add(30, 40);
i.sub(30, 10);
Console.ReadLine();
}
}



