Dec 29, 2019

INTERFACE (class and interface) IN C#

c# interface, interface, interface in java, class and interface
interface in c#

INTERFACE

            “Interface is mechanisms in which we declare abstract members only, and it’s allowing us to build multiple inheritances.”
SYNTAX
Public Interface I<name>
{
  Defining abstract members;
}
Public class <name>: I<name>
{
  Implementation of abstract member;
}

POINTS TO DEFINE INTERFACE

            Some important points that should keep in mind when creating an interface.
        1.    All the member of interface must abstract, non-abstract member is not allowed.
        2.    In the child class of interface the abstract member should implement.
        3.    The member need not to declare public as well as abstract just like we done in abstract class,                here in interface all the members are public and abstract by default.
        4.    The name of interface is usually start with “I” capital I.
        5.    It allows us to use multiple inheritances in c#.
        6.    An interface can be inherited from another interface.
        7.    It’s not require to override the member but it’s up to you, you can override but not necessary.
        8.    At a time we can inherit a class and an interface in a child class.
EXAMPLE
            Public Interface IEvaluation
            {
              void Add (int a, int b);
              void Sub (int c, int d);
            }
            Public class Implementation: IEvaluation
            {
              Public void Add(int a, int b)
              {
                Console.WriteLine (a + b);
              }
              Public void Sub(int c, int d)
              {
                Console.WriteLine (c - d);
              }
            }
            Class Program
            {
              Static void main(string [ ] arg )
              {
                Implementation Obj = new Implementation ( );
                Obj. Add(24, 25);
                Obj. Sub(34, 76);
                Console. ReadKey ( )
              }
           
c# interface inherit from abstract class,interface methods in c#, difference between interface and abstract class and class
interface and abstract class

          DIFFERENCE BETWEEN CLASS, ABSTRACT CLASS AND                                                               INTERFACE


CLASS
Class contains only non-abstract members.
Class declare with the keyword ‘Class’ i.e.
<Modifier> class <Name>
{ }
Class member can be implemented at the current class i.e.
<Modifier> class <Name>
{
Implementation;
}
No child class need to implementation, it’s done with declaration time.
ABSTRACT CLASS `
Abstract class contains abstract and non-abstract members.
It declare with keyword ‘abstract class’ i.e.
<Modifier> abstract class<Name> { }
Abstract member of class cannot implement in the current class only declaration. The abstract member should declare with ‘virtual’ keyword
Child class needs to implement the abstract members.
INTERFACE
Interface contains only abstract class.
Interface is declare with the keyword ‘Interface’ i.e.
<Modifier> Interface <IName> { }
Interface member never implemented only declare in that interface.
Child class needs to implement the interface member.


0 comments:

Post a Comment

Please do not enter any spam link in the comment box