Query Regarding Interface

Posted by Akiii under C# on 3/6/2011 | Points: 10 | Views : 1976 | Status : [Member] | Replies : 8
Can anybody tell me what for interfaces are used in real life projects?
Why do we need an interface first of all ?
what are interfaces good for if they don't implement functionality ?
We can simply write a class and declare/define members in it, why do we we have to inherit an interface and then define it in class?
Please give some real world examples where interfaces are used.

Thanks and Regards
Akiii




Responses

Posted by: Gsolvers on: 3/6/2011 [Member] Starter | Points: 25

Up
0
Down
Great Question!

Interface are defined to bring standard across implementations. If we take a real life example can you tell me why most of the tyres of car are of same size? and why the clock always rotate right direction?

The above is because the standard interfaces are defined to make life easy for consumers. If a company manufacture a car which uses tyres of different dimension, in that case just imagine the pain for customer to buy a new set of tyre?

I can get you more information across. But just want to check whether you agree with this information?

Best Regards,

VG
www.TeacherJi.com

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhu.b.rokkam on: 3/6/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Check this link this might give you some idea

http://msdn.microsoft.com/en-us/library/3b5b8ezk.aspx

Thanks and Regards
Madhu

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 3/6/2011 [Member] Bronze | Points: 25

Up
0
Down
@Gsolvers .....
Thank you for your reply......
Yes i agree with you that we can implement multiple inheritance but we cannot do that with a class.....
can you give me more examples ?

@Madhu.b.rokkam......
Thank you for your reply......
i have read the content from that link....but please can you give me practical implementation in projects ?

Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gsolvers on: 3/6/2011 [Member] Starter | Points: 25

Up
0
Down
Define a interface
ISalaryCalculator
Declare methods: CalculateSalary

IN an organization you have different type of employees and there salary is get ting calculated differently especially bonus/tax rates etc.

To solve this you can implement ISalaryCalculator interface in EmployeeSpecific classes for e.g.

ManagerSalaryCalculator
PeonSalaryCalculator
ExecutiveSalaryCalculator


Now You can use simple factory pattern to return the object of particular class in your consumer implementation and use it.

Let me know if you need full code and language you want the code to be in.

Best Regards,

VG
www.TeacherJi.com

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 3/7/2011 [Member] Bronze | Points: 25

Up
0
Down
@Gsolvers.....

Thanks for your post.
Yes sure, it will be better for me to understand if i had the code......can u post that code in c# ??

Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gsolvers on: 3/7/2011 [Member] Starter | Points: 25

Up
0
Down
Sure, I will post a working one tomorrow.

Best Regards,

VG
www.TeacherJi.com

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhu.b.rokkam on: 3/7/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
check this link you have a sample explained here

http://www.codeguru.com/csharp/csharp/cs_syntax/interfaces/article.php/c7563

Thanks and Regards
Madhu

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 3/7/2011 [Member] Bronze | Points: 25

Up
0
Down
@Gsolvers ......
Thank you very much for agreeing to post the content, i hope it will benefit me to understand the implementation of interface and moreover we can discuss any doubt here.....

@Madhu.b.rokkam
Thank you very much for posting the links, i will go through it asap and will let you if any doubt arises....and i hope you will help me out....

Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response