10 Interface in C#
10 Interface in C#
operations/methods of an object.
▪ An interface is a completely "abstract class", which can only contain abstract
methods and properties (with empty bodies)
▪ Example:
// interface
interface Animal
{
void animalSound(); // interface method (does not have a body)
void run(); // interface method (does not have a body)
}
▪ Just like a class has another class as a Parent, a class can also have an Interface as a
Parent.
▪ If a class has an interface as a Parent, the class is responsible for implementing all
the abstract methods of the interface.
▪ Class: Contains only the Non-Abstract Methods (Methods with Method Body).
▪ Abstract Class: Contains both Non-Abstract Methods (Methods with Method Body)
and Abstract Methods (Methods without Method Body).
▪ Now, the Child class that inherits from Interface1 has to implement one method, i.e.,
Add, and the child class that inherits from Interface2 has to implement two
methods, i.e., Add and Sub.
▪ Every member of an interface should be //Implement only the Add method
implemented under the child class without fail public void Add(int num1, int num2)
(mandatory), but while implementing, we aren’t {
required to use the override modifier just like
we have done in the case of an abstract class. Console.WriteLine($"Sum of {num1} and {num2} is {num1 +
num2}");
▪ In this example, while implementing the }
method, we are using the public modifier, which
is required. If you don’t use public, it will treat }
the method as private, and you will get an error.
public class ImplementationClass2 : ITestInterface2
Example: {
//Implement Both Add and Sub method
interface ITestInterface1
public void Add(int num1, int num2)
{
void Add(int num1, int num2); {
} Console.WriteLine($"Sum of {num1} and {num2} is {num1 +
num2}");
interface ITestInterface2 : ITestInterface1
{ }
void Sub(int num1, int num2);
} public void Sub(int num1, int num2)
interface ITestInterface1
{
{
public class ImplementationClass1 : Console.WriteLine($"Divison of {num1} and {num2} is
{num1 - num2}");
ITestInterface1
{ }
}
// Additional method in the class
▪ We cannot create an instance of an interface, but
we can create a reference of an interface. The public void Divide(int a, int b)
interface reference is going to hold the child class
instance. We can only invoke the methods {
declared in the interface using the interface Console.WriteLine($"Division of {a} by {b} is {a / b}");
reference.
}
▪ Example:
}
using System;
class Program
namespace InterfaceExample {
operations.Multiply(5, 3);
// Class implementing the interface
public class MathOperations : IOperations
// Cannot access Divide method using the interface reference
{
// Implementation of the interface method // operations.Divide(10, 2); // This will cause a compile-time error
}
▪ Defining a Common Contract: To ensure that multiple classes provide a common
set of methods, properties, events, or indexers, you can create an interface to
define that contract.
▪ Implementing Multiple Inheritance: C# does not support multiple inheritance for
classes (a class cannot inherit from multiple classes) but supports multiple
interface implementation.
▪ Enforcing a Specific Structure
▪ Implementing Polymorphism