File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Why interfaces in JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why interfaces in JAVA" Watch "Why interfaces in JAVA" New topic

Why interfaces in JAVA

Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
Hi there,
Why do you need interfaces in JAVA ?
What is not possible, simply having class or abstract class ?
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

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.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
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.
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

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
Gillian Bladen-Clark

Joined: Dec 13, 2003
Posts: 18
As an example, a CharSequence is a readable sequence of characters. String, StringBuffer and java.nio.CharBuffer all implement the CharSequence interface. So if a method expects a variable of type CharSequence, you can pass it a variable of any of these types - String, StringBuffer or java.nio.CharBuffer.
[ December 19, 2003: Message edited by: Gillian Bladen-Clark ]
[ December 19, 2003: Message edited by: Gillian Bladen-Clark ]
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
You might appreciate a read of a few pages of author Bill Venners' in-progress-and-frequently-changed book Interface Design. Take a look at the sections in chapter 3 listed in the table of contents.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
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
Then drop modules, and returns to the first version of BASIC...
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

[ December 22, 2003: Message edited by: Igor Ko ]
I agree. Here's the link:
subject: Why interfaces in JAVA
It's not a secret anymore!