Interface and Abstract

Interface 

Implementing an interface consumes very little CPU, because it's not a class, just a bunch of names, and therefore there is no expensive look-up to do

Following are the difference between the Classes and Interface
  1. The interfaces are used in java or .net to implementing the concept of multiple inheritance whereas classes are not used to implement multiple inheritance.
  2. We are not allocating the memory for the interfaces whereas the memory is allocated for the classes
  3. Interfaces are always implemented whereas classes are always extended

 public interface IA
        {
            void fun();
            void funb();
        }
        public interface IB
        {
            void fun();
            void funb();
        }

        public class DC : IA, IB
        {
            public void fun()
            {
                Console.WriteLine("FunA");
            }

            public void funb()
            {
                Console.WriteLine("FunB");
            }
        }

        static void Main(string[] args)
        {
            IA obj = new DC();
            IB obj1 = new DC();
            obj.fun();
            obj1.funb();
 }


using System;

namespace MyInterfaceExample
{
    public interface IMyLogInterface
    {
        //I want to have a especific method that I'll use in MyLogClass
        void WriteLog();      
    }

    public class MyClass:IMyLogInterface
    {

        public void WriteLog()
        {
            Console.Write("MyClass was Logged");
        }
    }

    public class MyOtherClass :IMyLogInterface
    {

        public void WriteLog()
        {
            Console.Write("MyOtherClass was Logged");
            Console.Write("And I Logged it different, than MyClass");
        }
    }

    public class MyLogClass
    {
        //I created a WriteLog method where I can pass as parameter any object that implement IMyLogInterface.
        public static void WriteLog(IMyLogInterface myLogObject)
        {
            myLogObject.WriteLog(); //So I can use WriteLog here.
        }
    }


In my example, I could be a developer who write MyLogClass, and the other developers, could create they classes, and when they wanted to log, they implement the interface IMyLogInterface. It is as they were asking me what they need to implement to use WriteLog method in MyLogClass. The answer they will find in the interface.

Comments

Popular posts from this blog

Sites.Selected | Graph API SharePoint Permission

Configure the SharePoint Online App Catalog

Azure Function | Sharepoint List item | Call from Power Automate Flow