File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java Micro Edition and the fly likes Interfaces with method body? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Interfaces with method body?" Watch "Interfaces with method body?" New topic
Author

Interfaces with method body?

Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hi All,

I have started reading the J2ME specifications and found that the interfaces like RecordEnumiration has methods which returns or does some action. For instance, the numRecords() would give the number of records in the RecordEnumiration.

According to my knowledge methods in an interface has ONLY method signatures and no method bodies, that's why when you implement that you have to give the methods a body in your class. But now in this, there is no need to do that. Could you please explain how this happens. What I want to know is that, how these interfaces return values or do any actions.

Thanks a lot


SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

A method that returns a value of interface type, say RecordEnumeration, can't actually return an object whose runtime type is "RecordEnumeration", because you can't instantiate an interface, of course. It has to return an instance of a class that implements that interface. It doesn't have to be a public class -- it can be an instance of a package-private class, or an instance of an inner class, or anything. All you know is that you've been handed a RecordEnumeration, and you can call all of its methods. You don't know the "real" type of that object, but you don't care.

Look at the java.sql package (JDBC) which consists almost entirely of interfaces. You only ever deal with Connection, ResultSet, and Statement variables, not knowing that the objects you're working with are actually instances of classes named (for example) OracleTCPConnection, MysqlResultSet, or JdbcOdbcStatement.


[Jess in Action][AskingGoodQuestions]
Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hi,
Thanks a lot for the very clear answer, I understood the concept very clearly. Thanks a lot once again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces with method body?