tags:

views:

183

answers:

5

Duplicate: http://stackoverflow.com/questions/122883/interfaces-why-cant-i-seem-to-grasp-them

With regards to OOP,how would you describe an interface?

What i mean is , sub-classing can be described as "Has-A" and Inheritance could be "Is-A". A member method could be "Can-Do" ..

Is there any way this could be extended (no pun intended) to describe what an Interface does?

Thanks

+1  A: 

Acts-As-A.

chaos
+2  A: 

An interface is an abstract base class with all pure virtual members.

So looking at your Has-A/Is-A, it should be similar to whatever you would be apply for an abstract base class.

Interfaces generally exist in languages that do not fully support multiple inheritance as a way to more safely offer some of the same benefits.

Joel Coehoorn
A: 

Joel that isn't exaclty what an interface is. It is like a abstract base class in a way but it has no implementation of the methods and properties.

This pretty much sums up what an interface is.

http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_interrfaces03052006095933AM/csharp_interrfaces.aspx?ArticleID=cd6a6952-530a-4250-a6d7-54717ef3b345

Mike Geise
Hence the "all pure-virtual members" part.
Joel Coehoorn
+1  A: 

As is your description for methods, I would also describe an interface as a "Can-Do". An interface is a contract like "all the classes that implement me, can do these things".

Federico Ramponi
+3  A: 

I think of objects as nouns, methods as verbs, and interfaces as adjectives (of course this analogy is oversimplified, but frequently works well enough).

Example: an interface Serializable works like an adjective, in that it applies some qualities to an object that implement that interface, but does not change what that object is. We can say, "this is a serializable object." But we don't say, "this object is a serializable," nor do we say, "this object has a serializable."

I also like Federico's answer that an interface is "CAN-DO".

An interface is a group of related operations that the class supports. Together, the methods in an interface describe what the class can do.

Just like a noun can take multiple adjectives, a class can implement multiple interfaces, as long as they don't conflict. The union of all the interfaces a class implements is the sum of what the class can do.

In practical terms, an interface is a set of method signatures, without the code implementing those methods. Just the method name and arguments. Depending on the language, a method signature may also include return type, and exceptions thrown.

An interface consists of methods, but not data members.

BTW, I wouldn't say sub-classing is HAS-A. My understanding is that sub-classing is the same as inheritance, so these are both IS-A. Whereas HAS-A is called Aggregation or Composition.

  • Composition is where an object owns another object. Destroying the outer object also destroys the inner objects. Example: University composes Departments. Close the University, and the Departments disappear.

  • Aggregation is where an object includes another object, but does not own it. Destroying the outer object does not destroy the inner objects. Example: University employs Professors, but closing the University does not kill the Professors.

Bill Karwin