Link to home
Start Free TrialLog in
Avatar of RajG1978
RajG1978

asked on

Can anyone give sample example where I can implement Interface and in what scenarios I should choose abstract class over interface?

I know the differences between interfaces and abstract class(theoretical). I want to know particularly in .Net in what scenarios I should choose Interface and when I should use abstract sample. Please give me small example for both cases
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RajG1978
RajG1978

ASKER

Particularly in what scenarios can I interfaces? Example please.
Let me say I go for an interview and the interviewer asks me "Give me scenarios where you use Interface and where you would use Abstract class". What will be the answer.
You use both interfaces and abstract class to define a common set of properties and/or methods that different classes need to have so that you can handle them in a common way through polymorphism.

You use an abstract class when you can provide the implementation (code) of some of these methods and properties.

You use an interface when the methods and properties that you want to share all need to be defined (coded) inside of the classes that implement the interface.

Interfaces also have a second role. Since multiple inheritance (inheriting from 2 classes) is not permitted in .NET, an interface can be define to define a second set of common properties and methods that you want to share between classes that already inherits from another one, thus simulating multiple inheritance.