Interface:
* Interface is used as same as a class but interface contain no implementation. it have only declaration.
* interface by default is public. and it can not used access modifier.
* interface can not contain data member.
* interface support multiple inheritance.
* if interface class contain method and inherit from other class then all method must be implement in a derived class.
An interface can never be instantiated e.g. ICar car = new ICar(); It’s wrong.
It only contains signature of its methods.
An interface has neither constructor nor fields.
It is also not permitted to have access modifier on its methods.
Its members are always implicitly public and cannot be declared as virtual or static.
=========== what is explicit interface========
* a class inherit from two interface it contain same method then how could identified the method.
then its used explicit interface.
Note:
explicit implementation is that an explicitly implemented member cannot be accessed using a class instance,
but only through an instance of the interface.
Example of Interface:
class Program
{
static void Main(string[] args)
{
AbsA t = new TestA();
t.MethodTest();
Console.ReadKey();
}
}
interface AbsA
{
void MethodTest();
}
public class TestA:AbsA{
public void MethodTest(){
Console.WriteLine("A");
}
}
Purposes of Interfaces:
1. Create loosely coupled software.
2. Support design by contract (an implementer must provide the entire interface).
3. Allow for pluggable software.
4. Allow objects to interact easily.
5. Hide implementation details of classes from each other.
6. Facilitate reuse of software.
No comments:
Post a Comment