Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - INTERFACE


So, far we have learnt if there is at-least one abstract behaviour in a class the entire class has to marked as abstract. That is the concept of abstract class. So in an abstract class there can be abstract(undefined) as well as non abstract(defined) methods.


Now the concept of Interface takes us one step ahead of abstraction.


i.e In an Interface all the behaviours/methods should be abstract. In other words it's a 100% abstract class.


Interface


An Interface is a 100% abstract class where all the methods must be abstract(i.e. undefined). Just like the abstract class an interface cannot be instantiated.


Story :

A new god came who was the designer of fish. And fish will breathe Oxygen from water. Now, the Supreme god was in problem, as he made this rule that all living beings will breathe oxygen from air.

abstract class LivingBeing
{
	public void breathe()
	{
		System.Console.WriteLine("Breathes oxygen from air.");
	}

	abstract void sound();// The sound method is incomplete.
}

Now, the breathe() method was not fixed as fish breathes oxygen from water. So, the master God changed his class to an Interface:


interface LivingBeing{

	void breathe(); // Undefined

	void sound(); // Undefined
}

But we have learnt if a method is not defined we need to mark it as abstract. But in the above case we didn't do it, why?


It's because LivingBeing is now an interface. And as I said, in an interface by default all the behaviours/methods are declared as abstract. So, in this case the compiler converts it for you to abstract :


interface LivingBeing
{
	public abstract void breathe(); // Undefined
	public abstract void sound(); // Undefined
}

And below is the declaration of Fish class:


class Fish : LivingBeing
{
	public void breathe()
	{
		System.Console.WriteLine("Breathes oxygen from water.");
	}

	public void sound()
	{
		System.Console.WriteLine("It squeaks.");
	}
}

So, in the above scenario we have seen the : keyword, which is used to inherit a base class :


class Fish : LivingBeing

Story :

And the beauty of it is, the Supreme god had declared this rule that all living beings should make some kind of sound to communicate and breathe to survive. And his fellow gods has to obey this rule by defining this behaviour in their own classes. Thus we have seen in Fish class, the sound() and breathe() behaviour had to be defined else it would have ended up with some kind of Error.

Now, let us write the class which creates the objects :


Example :



interface LivingBeing
{
	public abstract void breathe(); // Undefined
	public abstract void sound(); // Undefined
}

class Fish : LivingBeing
{
	public void breathe()
	{
		System.Console.WriteLine("Breathes oxygen from water.");
	}

	public void sound()
	{
		System.Console.WriteLine("It squeaks.");
	}
}

class CreateLivingBeing
{
	public static void Main(string[] args)
	{
    	Fish fish = new Fish();
		fish.breathe();
		fish.sound();
	}
}


Output :



  Breathes oxygen from water.
  It squeaks.

Multiple Interface Inheritance


Although multiple Inheritance is not supported by C#(i.e. A C# class cannot inherit from two C# classes). However, multiple Interface Inheritance can be achieved in C#.


Let us look at the below example :


Story :

Say there is a master God who has prepared an Interface named LivingOrganism with two behaviors/methods, breathe(which is already there in LivingBeing Interface) and move (Like a fish swims, birds fly and humans walk).

Example :



interface LivingBeing
{
	public abstract void breathe(); // Undefined
	public abstract void sound(); // Undefined
}

interface LivingOrganism
{
	public abstract void breathe(); // Already present in 'LivingBeing' interface.
	public abstract void move(); // New method
}



And below is the declaration of Fish class which implements both LivingBeing and LivingOrganism:


Example :



class Fish : LivingBeing, LivingOrganism
{
	public void breathe()
	{
		System.Console.WriteLine("Breathes oxygen from water.");
	}

	public void sound()
	{
		System.Console.WriteLine("It squeaks");
	}
	
	public void move()
	{
		System.Console.WriteLine("It swims");
	}
}



In the above class, the move() method is newly defined. Although breathe() is present in both LivingBeing and LivingOrganism. But there is only one definition for breathe().


public void breathe(){

	System.Console.WriteLine("Breathes oxygen from water.");
}

Now, let us write the class which creates the objects :


Example :



interface LivingBeing
{
	public abstract void breathe(); // Undefined
	public abstract void sound(); // Undefined
}

interface LivingOrganism
{
	public abstract void breathe(); // Already present in 'LivingBeing' interface.
	public abstract void move(); // New method
}

class Fish : LivingBeing, LivingOrganism
{
	public void breathe()
	{
		System.Console.WriteLine("Breathes oxygen from water.");
	}

	public void sound()
	{
		System.Console.WriteLine("It squeaks");
	}
	
	public void move()
	{
		System.Console.WriteLine("It swims");
	}
}

class CreateLivingBeing
{
	public static void Main(string[] args)
	{
    	Fish fish = new Fish();
		fish.breathe();
		fish.sound();
		fish.move();
	}
}


Output :



  Breathes oxygen from water.
  It squeaks.
  It Swims.

Note : An Interface can inherit from another Interface. A class can inherit from an Interface and a class. A class can inherit multiple Interfaces.