This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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?
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?
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...
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.