This week's book giveaway is in the Cloud forum.
We're giving away four copies of The Business Blockchain and have William Mougayar on-line!
See this thread for details.
Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces with method body?

 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks a lot for the very clear answer, I understood the concept very clearly. Thanks a lot once again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic