Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why use an interface? 2

Status
Not open for further replies.

bromberg

Programmer
Oct 14, 2002
24
0
0
US
What is the benefit of an interface since it has no code and therefore can't perform anything useful?
 
For one thing, interfaces are useful when you need to require a class to have specific methods.

Here's a situation I encountered just yesterday. I had an array of vendor objects and I wanted to sort them. I could just create a TreeSet with my array, but java would sort them in some "natural" order based on who knows what. I wanted them sorted by the vendor name.

So on my vendor class I implemented the Comparable interface. That forced me to create a compareTo method in my vendor object, in which I specifically compared vendor name to vendor name.

TreeSet is smart enough to know that if the objects it's sorting implement the Comparable interface, they will have a compareTo method to use to sort the objects. TreeSet doesn't need to understand about vendors or any other methods or properties on my objects. It can sort any object that implements Comparable.

 
Idarke:

For as many JAVA books I've read this is the first time a rel-life example has made this concept meaningful.

Thanks very much!
Danny
 
why hasnt anyone actually read anything in textbooks that doesnt uinvolve doing a GUI???

interfaces are the ESSENCE of OO programming. if you dont understand them DONT DO ANYTING ELSE UNTIL YOU DO!!!

jeez
 
funny, I thought Encapsulation, Polymorphism, Inheritence and Abstraction were the essence of OO (programming or otherwise).

I know of no other language that uses interfaces like Java does. C++ uses multiple inheritance (errk) which is not nearly as safe.

Interfaces are a way of avoiding the possibly dangerous multiple inheritance ideas in C++.

They provide a contract, telling any class that asks that the object which implements the interface must have all of the methods the interface does, what it does IN those methods is irrelevent, as long as it takes in and passes out the required objects (or void). Which is just what idarke said. -------------------------------------------
There are no onions, only magic
-------------------------------------------
 
From the great Dick Baldwin (Hero of all those who did not buy a java text) "I will suggest that there is little if anything useful that can be done in Java without understanding and using interfaces."

bromberg: have a look at:
(and the following article) - in fact - all of the tutorials on his site!

jfryer - Encapsulation, Polymorphism, Inheritence and Abstraction are FEATURES of an OO programming language. you are not REQUIRED to use them. multiple inheritance is the essence of OO programming, and interfaces are the way to do it with Java.
<<< Why is everyone trying to get a quick fix???? If you bothered to LEARN the theory it would have worked the first time around!!!! Go to uni! >>>
 
DannyWild...thanks for the link (now bookmarked): it helped clarify and made for an interesting read

I still maintain that most intro Java books I've read provide very little motivation for the importance of an &quot;interface&quot;. I like JFryer's comment about it being a 'contract'. But still, on the surface it looks like a 'do nothing' class and I think authors should show the downside if Java didn't provide such a construct.
 
&quot;multiple inheritance is the essence of OO programming&quot;

I think that the OBJECT concept is the essence of OOP... And the multiple inheritance is not a key feature.

Java does't allow multiple inheritance and the interfaces are NOT the way to do it. As you implement an interface you are not inheriting the code, you are implementing it, so the way to do something similar to multiple inheritance is an Adapter Pattern.

The power of the interface concept resides in the possibility of use and design a framework. For example the Collection Framework. Check this:


Interfaces are one of the most powerful features of java, and will take a long discussion to explore all of its advantages.


Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
i appear to have been misinterpreted although i can understand how i gave this impression. i was trying to get across that the designers of java used interfaces in order to avoid the complexities of multiple inheritance.

- dont bother with a quick fix. head in book all day breeds good results
 
The fog clears and understanding dawns!

dannywild, with your last statement I can agree with you. Totally.

And yes you where misinterpreted, by me at least.
[2thumbsup] -------------------------------------------
There are no onions, only magic
-------------------------------------------
 
You are right dannywild82, I misinterpreted you. Anyway, who can be absolutely right?
Best regards. Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top