Monday, August 14, 2017

Why C Sharp Supports Multiple Inheritance with Interface only.

Multiple Inhertiance

A class which derived from more than one class it is called Multiple inheritance

-----------------------------------------------------------------------------------------
Example:


public class Employee
    {
        public virtual void DoWork()
        {
            //Do work
        }
    }

    public class Manager:Employee
    {
        public override void DoWork()
        {
            //Manager's implementation of do work
        }
    }

    public class Developer : Employee
    {
        public override void DoWork()
        {
            //Deveoper's implementation of do work
        }
    }

    public class JuniorDeveloper:Manager, Developer// Compile Time Error :Cannot have multiple base classes but assume that it were possible
    { }


----------------------------------------------------------------------------------


public class A
    {
        public virtual void A_Method()
        {
            Console.WriteLine("Class A Method");
        }
    }

    public class B : A
    {
        public override void A_Method()
        {
            Console.WriteLine("Class B Method");
        }
    }

    public class C : A
    {
        public override void A_Method()
        {
            Console.WriteLine("Class C Method");
        }
    }

    public class D : B, C  // If Multiple inheritence is possible in C# then
    {
        public override void A_Method()
        {
            Console.WriteLine("Class C Method");
        }
    }


    public static void main()
    {
        D objD = new D();
        objD.A_Method();// Making object of class D and calling overloaded method A_method will 
                        //confuse the compiler which class method to call as both inherited class methods has been overloaded.
    }

Explanation:
In the code above we have the A_Method()
which is defined by class A. But, the problem is that class D derives from both classes B and C, which both derive from class A. This means that there are essentially 2 copies of the A_Method()
that are available because there are 2 instances of A in D’s class hierarchy.
And the compiler will give an error and say that the reference to A_Method() is ambiguous.

-------------------------------------------------------------------------------------

Step1 Interface Definition

public interface IEmployee
{
   string GetEmployeeId();
}
The above code contains a method signature named DoWork ().
 Step 2 : Interface Implementation
As I have already defined an interface IEmployee in the above code snippet. Now if I want to implement the interface to my class Engineer as shown below.
public class Engineer:IEmployee
{

}
Step 3: 

Now if I execute the above code I will get a compile time error


-----------------------------------------------------------------------------------



Step 4:

The correct implementation of the above code is shown below



As we can see from the above code the signature of the GetEmployeeId() method in the Engineer class is same as the signature of the method in the (IEmployee)Interface.

Step 5:
Interface can only “inherit” from the interface only. Suppose if we want to extend the implementation of an interface in some part of our project without disturbing the already existing interface, in that case we can create a new interface and inherit it into new interface as shown below.

Example:

public interface Ibase1
    {
        void message();
    }
    public interface Ibase2
    {
        void message();
    }
    public class child : Ibase1, Ibase2
    {
        public void message()
        {
            Console.WriteLine("Hello Multiple Inheritance");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            child obj = new child();
            obj.message();
            Console.ReadLine();
        }
    }


Output:


Important Points to Remembers 
    Class FirstClass { }
    Class SecondClass { }

interface X { }
interface Y { }
You can inherit like the following:

class NewClass : X, Y { }
In the above code, the class "NewClass" is created from multiple interfaces.


class NewClass : FirstClass, X { }
In the above code, the class "NewClass" is created from interface X and class "FirstClass".

WRONG:

class NewClass : FirstClass, SecondClass  { }
C# doesn't support Multiple inheritance

The above code is wrong in C# because the new class is created from class "FirstClass" and class "SecondClass". The reason is that C# does not allows multiple inheritance.



Finally Note: 
Point 1: 
When we use the Multiple inheritance , we use more than one class. Lets  us Assume one condition  like below
class A and class B are base classes and class c is is multiple inherting it and where class c is inheriting a function ,ok it may be possible that this function with same name and same signature can present in both class A and Class B .That time how the compiler will know that which function it should take wheather from class A or class B.
So this time Multiple inheritanc won't work .So avoiding this problem we use Interface. In interface we just declare a function  and in derived class we write the implemenation.

Point 2:
C# does not allow multiple inheritance because of ambiguity.



1 comment:

  1. Really nice blog post.provided a helpful information.I hope that you will post more updates like this
    Dot NET Online Training Bangalore

    ReplyDelete

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...