-3

Possible Duplicate:
Interfaces: Why can't I seem to grasp them?

I have seen many different types of answers on what is interface (techical, c#) and why is it there.etc..

Today i want to ask you guys question whether below my understanding is correct ?

You will implement an Interface on class A when you want your consumer classes (say classes B,C,D,E) to use class A for a reason (and that reason is Interface name).

Lets say class A has functionality to can Delete Employee work history Data and as well it can delete Employee skillset data. This class A has non null property called Employee_Id

Now you will implement 2 interfaces IDeleteWorkHistroy and IDeleteSkillSet and implement them in class A

All the consumer classes B,C,D... will talk to your class only through these two interfaces and they wont directly use instance of class A anywhere in the application

e.g. in class B wants to delete only employee work history: IDeleteWorkhistory EmpWH = new class A(emp_id); and everywhere else in class B you will use EmpWH and nothing else of class A.

Basically consumers of your class will use only that thing which he needs or other way consumer will talk to that interface which is needed by him.

In other words (keyboard is interface of your Computer) if you want to talk to your computer you wont need a bit sequencer where you you pass bit by bit information to CPU using interrupts..(though , ideally , you can do it). You will talk to your machine using proper interface only.

So any class that you are writing , write proper interfaces so that the consumers will talk to your class in standard way.

Please let me know if my understanding is correct or needs modifications ?

Community
  • 1
  • 1
Dhananjay
  • 3,673
  • 2
  • 22
  • 20

3 Answers3

1

I think your understanding is right. I generally see interfaces as markers of a functionality. A class may have more than one functionality, and when you inherit interfaces, it is like saying I am able to do this. Some of the consumers, may not need all of the functionality or may not need the same functionality with another one.

On the other hand, this is not the only use of interfaces. Sometimes you use them for somehow implementing multiple inheritance, sometimes for IOC purposes. But in your context, I think you are right.

daryal
  • 14,643
  • 4
  • 38
  • 54
1

An interface is basically a contract. By implementing an interface in a class you are saying that that class will provide certain methods.

For example, if you create a class and utilise the IDisposable interface, then you are saying that your class will provide the methods IDisposable specifies such as, Dispose().

Try reading this https://web.archive.org/web/20211020203222/https://www.4guysfromrolla.com/articles/110304-1.aspx

robsmithuk
  • 11
  • 1
  • I think your understanding is basically correct, although I think your use of terminology is a little off. For example, you say "All the consumer classes B,C,D... will talk to your class only through these two interfaces and they wont directly use instance of class A anywhere in the application" class A will need to exist as an instance, the instance will just include the interface and they will only use those methods. Unless you use static methods and you can call those directly from the class. – robsmithuk Mar 22 '12 at 10:30
  • That's why I said "they wont [directly] use". Indirectly they are using and they have to. – Dhananjay Mar 22 '12 at 12:39
0

Actually, I've seen interfaces more like a "template". For example: You want to make sure that a class provides certain functionality. An interface allows the person using classes to concentrate on the functionality rather than how it's implemented.

Example: Lists
A list needs methods to add, search and delete items and to get an item at a certain position. This could be turned into an interface.

Now there could be multiple implementations of the interface that internally work differently. For exmple there could be an ArrayList that manages the list as an array, a SingleLinkedList which implements a linked list or a DoubleLinkedList. All these implementations are different and have for example different performance too.

If you are to write a method that sorts a list, you don't care which type of list is being given - all you need to know is that the implementation implements the methods declared in the interface.

BUT: It doesn't make sense to create an interface for every class you write! Sometimes a proper class hierarchy or abstract base classes are enough.

EDIT
I do not think your understanding is totally correct. You're saying the goal should be to use as little functionality of a class as possible. While it is correct that interfaces can be used to achieve what you're describing, the given scenario is not what is desired in most cases and it is not the primary goal of interfaces.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139