You need interfaces becase sometimes a class needs to act as two (or more) things. C++ solves this problem through multiple inheritance; Java providces interfaces. (In my opinion, interfaces are the cleaner manner, although they can, at times, be frustrating, which is why some people clamor for Java to have Multiple Inheritance...) For example, say you had a Mammal class which extends Animal. Subclasses of Mammal could be Bat, Dolphin, and Human. Now, you have several Animals contained in an Ocean object. Animal in this environment need to be able to swim, but the Animal class does not have a swim() method, because not all Animals can swim. Enter the Swimmer interface. Dolphin and Human implement the Swimmer interface (as well as all Fish), and the Ocean class now can only accept Swimmer swimmers. (It can use run-time type checking to reject anything that is not a Swimmer, or it can cause it to drown...) [ December 17, 2003: Message edited by: Joel McNary ]
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
It's often said interfaces exist in Java as a substitute for multiple inheritance. That is, you can only extend one class in Java while some other languages let you extend more than one class. I'd rather say interfaces can be used as a work-around for only being able to extend one class, but they have some other neat value. When you extend a superclass you may well be giving yourself dependencies on how the superclass is implemented, whether you realize it or not. Future changes to the superclass could break your class. Subtle behaviors in the superclass may confuse the heck out of you, too. Interfaces are a safer way to declare what kind of behavior you support. This may get pretty advanced in a hurry, but take a look at Allen Holub's Why Extends Is Evil for more details. Don't panic - the title is a deliberate exaggeration to draw you into reading the story.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I'll echo what they said a bit simpler, you can't extend from more than one class but you can implement more than one interface. When you look at interfaces like serialisable you will see that if you could only extend then you would have to have control of the first class you are extending from all the way up the chain to make that class extend from serialisable.
Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
Sometimes you want to describe what a class will do -- in the form of method (or function) signatures -- without committing to any implementation at all. The reasons why may be unclear until you've written some code that is flexible enough to be applied a variety of ways, but is written in a way that inhibits reuse, other than by cutting and pasting. With object-oriented code in particular, the ability to design a piece of software without investing time in coding is a prime attribute. In Java, interfaces help promote that capability. On the practical level, Java interfaces are designed to foil C++ features like multiple inheritance, as mentioned above. There are really quite a few benefits to using interfaces. It may be a while before you come to appreciate them, but the advantages are unmistakable once you know what you are looking for.
Make visible what, without you, might perhaps never have been seen. - Robert Bresson
Of course you can drop interfaces from compiler, so it will be return to 15 years - likes it was in the first version of C++ without multiple inheritance. It will be feable language design. Next step probably will be drop class at all, and return to Algol-60 Then you should drop modern version of if, while, do, and return to verison with labels IF(x) 100, 110, 120 IF(I.LT.5) GOTO 579 (FORTRAN IV), Then drop modules, and returns to the first version of BASIC... Assembler Computer code, and works from computer panel. ;-) I know man that used to input his program from computer panel Pdp-11 it seems like play in piano. And program was big - about 30 (2 bytes) words