Click here to Skip to main content
15,847,583 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Please let me know how the use of Interface makes difference....
one quick question how really Interface is useful for a sorting algorithm for any type of object.....

Programming against an interface makes it easy to replace the implementation at a later time. The interface defines the contract between the implementing class and the code that uses the class. The code that wants to use an implementor of an interface can even be totally oblivious of the concrete type of the implementor.
I like to use interfaces in combination with dependancy injection (also known as IoC (inversion of contgrol)). The class A that needs to use an implementor of interface IxxxYYY declares a property or maybe a constructor parameter of type IxxxYYY and when my IoC container instantiates class A any class that satisfies the IxxxYYY contract can be plugged in.

In regard to your question of using an interface for a sorting algorithm I'd say that having an interface here would make it easier at a later date to exchange the sorting algorithm with a better, faster, more advanced implementation.

Still have questions? Leave me a comment.

Share this answer
Espen Harlinn 4-Feb-11 11:32am    
Nice educational answer, 5+
siva455 8-Feb-11 6:25am    
I really didn"t get how using interface makes difference..(i mean when we need to choose interface)..
if possible please let me know with a real time sample....
Manfred Rudolf Bihy 8-Feb-11 6:40am    
Please be more specific. Which part of my answer are you having difficulties with?
siva455 9-Feb-11 2:19am    
please try to explain with simple scenario as the example which you have given is bit complex for me to understand basically that
IOC which you have mentioned in the post....
An Interface is going to be definition for your sorting algorithm - it is going to define the basis for sorting. It is not going to contain any sorting algorithm (nor is it going to do the sorting for you).

Basically you will create your interface and implement sorting algorithm A using this interface.
Tomorrow, you may find sorting algorithm B to be better than A, and so you are going to implement sorting algorithm B, using the same interface so that the code implementing the sorting need to change.

So, no an interface is not really going to help you in sorting. It is going to give you an extensibility mechanism for changing your sort algorithm later.

Share this answer
Manfred Rudolf Bihy 4-Feb-11 9:57am    
More or less what I meant. 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900