Hello Friends,
Doubt in Interface:
1. How do we use Interface in real time scenario.
2. How to assign Interface for a given Class?
3. How to inherit two Interface in a class?
4. Why do we use interface instead of creating object for a class and using in another class?
Thanks In Advance :)


Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Mar 3, 2014, 9:53 PM EST
1. How do we use Interface in real time scenario.
2. How to assign Interface for a given Class?
Answer for both the question is Here, the real time usage of interface in Log4Net.
3. How to inherit two Interface in a class?
You can do like, public class C : A, B // A & B are interfaces here
4. Why do we use interface instead of creating object for a class and using in another class?
As mentioned in the answer 1 & 2, interface is just a contract which should be implemented in the Class which is to be initiated.
saurabh mittal152 reputationPosted Mar 3, 2014, 10:06 PM EST
1. we can useinterface like this:-
interface IOurDevices { void connectToDevice(); }
2.to assign interface to class just inherit interface class and define the method:-
class USBDevice : IOurDevices
{
void connectToDevice() { connectionState = "connected"; }}
3.class USBDevice : IOurDevices,GenericDevice
{
//define ur methods
}
4.for security reasons we use interface beacause we can't create an object of
interface so there is no communication at runtime.
Munesh SharmaRank #174 · 13.9K reputationPosted Mar 3, 2014, 10:35 PM EST
Firstly i will tell you why we use interface.
(1):- Interface allows us to develop loosely coupled system.
(2) :- Interface are very useful for dependency Injection.
(3) :- Interface makes unit testing & mocking easier.
interface can't have implementation for any of its method.There is no access modifiers.
declaring Interface
Public interface interfacename
// interface members
int id;
char Id;
How to inherit two Interface in a class
public interface ITest { void Test(); } public interface ITest2 { void Test(); } public class Dual : ITest, ITest2 { void ITest.Test() { Console.WriteLine("ITest.Test"); } void ITest2.Test() { Console.WriteLine("ITest2.Test"); } }Abhishek KumarRank #510 · 4.8K reputationPosted Mar 4, 2014, 2:51 AM EST
1. How do we use Interface in real time scenario.
Interface is used basically where we don't want our client to know about the changes proposed. Interface creates a layer where you can add new functionality without doing much code change in client.
2.How to assign Interface for a given Class?
below is the interface which is returning the brand name and accepting car id as a parameter.
Interface Icar
{
string GetCarDetail(int Carid)
}
Interface IBike
{
string GetBikeDetail(int Carid)
}
Implementation
public class car :Icar
{
}
3. How to inherit two Interface in a class?
Public Class Vehicle :Icar,IBike
{
}
4. Why do we use interface instead of creating object for a class and using in another class?
Abstraction can be achieved by doing this.
Thanks