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.
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."
(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 "interface". 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.
"multiple inheritance is the essence of OO programming"
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:
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
dannywild, with your last statement I can agree with you. Totally.
And yes you where misinterpreted, by me at least. -------------------------------------------
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.