wood burning stoves
The moose likes Java in General and the fly likes i really don't understand interfaces.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "i really don Watch "i really don New topic

i really don't understand interfaces....

Ram Mohan
Ranch Hand

Joined: Jan 19, 2004
Posts: 44
yeah...i know im pretty dumb...but still, even after using java for quite some time, i don't understand how the interfaces in java API have methods??? aren't interfaces supposed to separate the abstraction from the implementation (or something similar to that) ?
thanks all

If quitters never win, and winners never cheat, who's the idiot that said: Quit while you're ahead?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Hi Ram,
You're not dumb.
An interface is a list of methods that a class should have, without the code to implement them. When a class implements that interface, it provides implementations of those methods.
So, for example, there's an interface java.util.List. I can write a method that accepts a List as an argument, and in my method I can call any List method on that argument. I can do to that argument anything that you can legally do to a List.
But I can't create a List and pass it to that method; List is only an interface. I have to create an instance of a class that implements List, like LinkedList or ArrayList. Each implementation has some benefits and some drawbacks. I choose the one most appropriate for the situation, create an instance of it, and pass it as an argument to that method.
Anytime you see an interface in the APIs, you must realize that the same thing is going on. There are going to be multiple different implementations, and you won't know specifically what class you're dealing with, only that it implements that interface.
A concrete example: when you get a java.sql.Statement object, it's not an instance of Statement; it may be an instance of OracleStatement, or MysqlStatement, or SybaseStatement, a class written by whoever implemented the driver you're using. But when you get a Statement, you don't know or care; you just treat it as a statement.
Get it?

[Jess in Action][AskingGoodQuestions]
Ram Mohan
Ranch Hand

Joined: Jan 19, 2004
Posts: 44
yeah i get it....ur list example was not so enlightening, but the JDBC example made it pretty clear (probably becoz i've not worked on lists enuf also)....i would also appreciate if you could give me some more info on this particular topic as RMI seems to be using interfaces a lot and i really don't understand who implements the getter & setter methods in a Bean...
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
You might want to read up on these:
Some people think you should create and implement interfaces as much as possible and try to avoid inheritance like the plague...well, almost!
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Consider an interface a contract between you and a client.
The contract tells the client what he can expect of you in terms of items to be produced and what input will be required for that.
In the same way an interface tells a method what methods (output) are available on a class implementing that interface and what parameters (input) those methods take.

I agree. Here's the link: http://aspose.com/file-tools
subject: i really don't understand interfaces....
It's not a secret anymore!