Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why interfaces in JAVA

 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
Why do you need interfaces in JAVA ?
What is not possible, simply having class or abstract class ?
thanks
siva
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gillian Bladen-Clark
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Igor Ko
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

[ December 22, 2003: Message edited by: Igor Ko ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic