Interface
An interface is a signature.It is not a class
Properties
It contains definitions or signature of a method and the implementation of these methods are provided by the class inheriting the interface
A class can implement more than one interface , so interface can be used to implement multiple inheritance in c#
No object of interface can be created
It contains only properties, indexers, methods, delegates and events signature.
It cannot contains constants members, constructors, instance variables, destructors, static members or nested interfaces.
Members of an interface cannot have any access modifiers even public.
Implicitly, every member of an interface is public and abstract. Also, you are not allowed to specify the members of an interface public and abstract or virtual
It cannot be instantiated but it can be referenced by the class object which implements it. Also, Interface reference works just like object reference and behave like as object.
IStore IObjStore = new Document();
ICompress IObjCompress = new Document();
An interface can be inherited from one or more interfaces.
An interface can extend another interface.
A class or struct can implements more than one interfaces.
A class that implements an interface can mark any method of the interface as virtual and this method can be overridden by derived classes.
Why Interface?
Interface based design concept provides loose coupling
Code reuse becomes easier
easier maintaniability
componenet based programming
//Interface called IAddCallView created which inheriting from 2 more interface
public interface IAddCallView:Iview,IAddView
{
// interface members
void showTransaction();
double getAmount();
//Properties created
string FirstName {get; set;}
string LastName {get; set;}
}
//Our class implementing interface
Public Class Student:IAddCallView
{
Public void showTransaction()
{
//Content goes here
}
//Interface defined
Public void getAmount()
{
//Content goes here
}
//Properties defined
Private string firstname;
Public string FirstName {
get{ return firstname;}
set{firstname=Value; }
}
}
How to make an interface private?
An interface could be private within another class
public class MyClass
{
private interface IFoo
{
int MyProp { get; }
}
private class Foo : IFoo
{
public int MyProp { get; set; }
}
public static void Main(string[] args)
{
IFoo foo = new Foo();
return foo.MyProp;
}
}
Types of Interface
1,Implicit Interface
2,Explicit Interface
Implicit Interface
The methods are publicly implemented
public class Person : IDal
{
public string FirstName { get; set; }
public string LastName { get; set; }
public void Add()
{
throw new NotImplementedException();
}
public void Update()
{
throw new NotImplementedException();
}
}
Explicit Interface
In explicit interface the methods are privately implemented
public class Person : IDal
{
public string FirstName { get; set; }
public string LastName { get; set; }
void IDal.Add()
{
throw new NotImplementedException();
}
void IDal.Update()
{
throw new NotImplementedException();
}
}
Explicit interface is a bad approach
Instead of this we can use two separate interfaces.
It can be used when your concrete class abstraction does not include interface abstraction.
public interface IDal
{
void Add();
void Update();
}
public class Person : IDal
{
public string FirstName { get; set; }
public string LastName { get; set; }
void IDal.Add()
{
// Add code goes here
}
void IDal.Update()
{
// Update code goes here.
}
}
//Only Properties ie public properties are available to Concrete class
Person objP=new Person();
objP.FirstName
objP.LastName
//For interface class interface methods are available.
IDal objID=new Person();
objID.Add();
objID.Update();
Comments
Post a Comment