Monday, May 23, 2016

Interface C#

Interface 

    interface IBankCustomer
    {
        void DepositMoney();
        void WithdrawMoney();
    }

    public class Demo : IBankCustomer
    {
        public void DepositMoney()
        {
            Console.WriteLine("Deposit Money");
        }

        public void WithdrawMoney()
        {
            Console.WriteLine("Withdraw Money");
        }

        public static void Main()
        {
            Demo DemoObject = new Demo();
            DemoObject.DepositMoney();
            DemoObject.WithdrawMoney();
        }
    }

  • Interface No need access modifiers, default is public.
  • Interface methods only header, no body.
  • Interface No Field.
  • Interface No Constructor.
  • Interface can inherit other interface.
  • Child Class must implement all the Interface method.
  • Child Class can inherit mutiple Interface.

Different Interface and abstract Class

1.A class cannot inherit more than one class (class DerivedClass : BaseClass1, BaseClass2),
interface can (public class Demo : Interface1, Interface2)

2. Abstract class can have abstract or non abstract method;

No comments:

Post a Comment