Thursday, January 13, 2022

What is interface

interface is just like a class. It is having blueprint of class.


ref:-  https://www.w3schools.com/cs/cs_interface.php

What is abstract class and interface

  •  abstract classes are fast vs interfaces are slow.
  • abstract class have single class inheritances vs interface is having multiple class inheritances
  • abstract keyword for creating abstract class and interface keyword for creating  interface
  • both(interface and abstract class) can not be instanced.
  • abstract class is having abstract functions and override is used with function that is having inheritense.
  • abstract class may or may not have abstract methods but abstract methods are defined in abstract class and where we inherit the class in other class, abstract method will be overrided there with override keyword.
  • Also in abstract class we cannot define abstract methods and they are defined in inherited class with override keyword to methods.
example:
public abstract class Animal
{
    public abstract string Sound { get; }
 public virtual void Move()
    {
        Console.WriteLine("Moving...");
    }
}
 
public class Cat : Animal
{
    public override string Sound => "Meow";
 public override void Move()
    {
        Console.WriteLine("Walking like a cat...");
    }
}

interface:- by default public members

abstract class:- by default  public and private members


reference :- https://www.educba.com/c-sharp-interface-vs-abstract-class/

https://www.geeksforgeeks.org/c-sharp-abstract-classes/