Wednesday, 4 December 2019

Interface

A interface is also a user-defined type. Interface can have methods, properties, events, and indexers as its members. But interfaces will contain only the declaration of the members. Interface contains only Abstract Methods (Methods without method body)
Every abstract method  of an interface should be implemented by the child class of the interface without fail (Mandatory).

Note: A class can inherit from a class and interface at a time.
Note: We can't declare any fields/variables under an interface.

The default scope of the member of an interface is public whereas its private in case of a class. By default every member of an interface is abstract so we don't require to use abstract modifier again just like we do in case of abstract class.

Code Example:
interface ITestInterface1
    {
        void add(int a,int b);
    }
    interface ITestInterface2 : ITestInterface1
    {
        void sub(int a, int b);
    }

    class ImplementationClass : ITestInterface2
    {
        public void sub(int a, int b) //public modifier is mandatory
        {
            Console.WriteLine(a - b);
        }

        public void add(int a, int b)
        {
            Console.WriteLine(a + b);
        }
        static void Main(string[] args)
        {
            ImplementationClass obj = new ImplementationClass();
            obj.add(10, 20);
            obj.sub(20, 10);

            //Or we can create an parent instance using child reference
            ITestInterface2 i = obj; //i is an parent instance creating using child "obj" reference
            i.add(30, 40);
            i.sub(30, 10);
            Console.ReadLine();
        }

    }

Abstract class in c#

An abstract class in an incomplete class or special class and we can't be instantiated. The purpose of abstract class is to provide a blueprint for derived classes and set some rules what the derived classes must implement when they inherit an abstract class.
1. Cannot create an instance of abstract class
2. If a class is an abstract class then it can contain abstract methods and non-abstract methods.
3. If we declare method as a abstract in base class (abstract class) then it is mandatory for all derived class to implement abstract methods.

Abstract Class Contains:
--Abstract Methods
--Non-abstract Methods

Code Example:-

public abstract class AbstractClassExample
    {
        public abstract void add(int a, int b); //abstract method
        public abstract void sub(int a, int b); //abstract method

        public int div(int a, int b) //non-abstract method of parent class
        {
            return a / b;
        }
    }

 
    class ChildClass : AbstractClassExample
    {
        public override void add(int a, int b)  //mandatory to implement
        {
            Console.WriteLine("Addition:" + (a + b));
        }
        public override void sub(int a, int b)  //mandatory to implement
        {
            Console.WriteLine("Substraction:" + (a - b));
        }

        public void mul(int a, int b)   //method of child class
        {
            Console.WriteLine("Multiplication:" + (a * b));
        }
        static void Main(string[] args)
        {
            //AbstractClassExample abc = new AbstractClassExample(); //cannot create the instance of abstract class

            ChildClass c = new ChildClass();
            c.add(10, 20);
            c.sub(20, 10);
            Console.WriteLine("Division:" + c.div(20, 4));
            c.mul(10, 5);
            Console.ReadLine();
        }
    }


Output: