Sunday, May 27, 2018

When to Use Interface in Real Projects

It is also user-defined type like a class which only contains abstract members in it and these abstract members should be given implementation under a child class of an interface. 

Note: A List, Dictionary, or Hash Table  are derived from an Interface, IEnumerable.


C#, multiple inheritance is not supported. When there is a situation like multiple inheritance, use Interface.





The solution of the multiple inheritance can be provided by Interface. So, we can do this example using Interface as follows. In this way, the diamond problem of Inheritance is solved.



Now, we will learn  where to use Interface. 


Now let us do this example with Classes first so that you can understand where exactly interface comes into picture.



public class Bike
    {
        public string Wheel()
        {
            return "2 wheeler";
        }
        public string Brakes()
        {
            return "Disc brakes";
        }
    }
    public class Unicorn : Bike
    {
        static void Main(string[] args)
        {
            Unicorn Toy = new Unicorn();
            Console.WriteLine(Toy.Wheel());
            Console.WriteLine(Toy.Brakes());
            Console.ReadLine();
        }

    }

Now i have one more class that is Shine Class, which will also inherit the Class Bike

public class Bike
    {
        public string Wheel()
        {
            return "2 wheeler";
        }
        public string Brakes()
        {
            return "Disc brakes";
        }
    }
    public class Shine : Bike
    {
        static void Main(string[] args)
        {
            Shine objShine = new Shine();
            Console.WriteLine(objShine.Wheel());
            Console.WriteLine(objShine.Brakes());
            Console.ReadLine();
        }
    }

Here  in the Shine Class i want to add a new method , So how we will Do it ?

How can we implement this, and in which way we can implement


CASE 1 - By Creating simple class


In that simple class, i am going to add a New Method  so that Shine Class can use it



 class NewFeatures

    {
        public void NoDiscBrakes()
        {
            Console.WriteLine("No Disc Brakes");
        }

    }

Now, inherit the Shine class from NewFeatures. , it was previously inherited from Bike class and for now, again 





It is giving me the Error, if i go with Classes


Case 2: Will create a Abstract Class and will try






CASE 3 - Direct creating a new method in the Shine Class


But this is not correct, because today i have one method, but they may be situation where you need 100 of methods to be changed. So it wont work



CASE 4 - By defining Interface

public class Bike
    {
        public string Wheel()
        {
            return "2 wheeler";
        }
        public string Brakes()
        {
            return "Disc brakes";
        }
    }
    public interface INewFeatures
    {
        void NoDiscBrakes();
    }
    public class Shine : Bike, INewFeatures
    {
        public  void NoDiscBrakes()
        {
            Console.WriteLine("No Disc Brakes");

        }
        static void Main(string[] args)
        {
            Shine objShine = new Shine();
            Console.WriteLine(objShine.Wheel());
            Console.WriteLine(objShine.Brakes());
            objShine.NoDiscBrakes();
            Console.ReadLine();
        }

    }








No comments:

Post a Comment

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...