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 ( )
}
}
interface and abstract class |
DIFFERENCE BETWEEN CLASS, ABSTRACT CLASS AND INTERFACE
CLASS
|
ABSTRACT CLASS
`
|
INTERFACE
|
0 comments:
Post a Comment
Please do not enter any spam link in the comment box