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.
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 :
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 :
Now, let us write the class which creates the objects :
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(); } }
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 :
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:
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 :
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(); } }