This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

interface method confusion  RSS feed

Tsehau Chao
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, after going through various code and APIs, one problem lead to another and right now I'm so confused that I'm not even sure if this is the right question!

I was given some sample code based on JDOM to work on XML files. It uses the collection framework which I am not familiar with (I was taught with JDK 1.4). In these sample codes (and many tutorials), I noticed many instances where an interface reference type invoked an interface method, yet I was unable to track down exactly which class implemented that interface.

In my n00b understanding, an interface method must be overridden somewhere! It does not make sense to use an abstract method. It is very frustrating and confusing to not be able to find that hidden class. Here is the code snippet in question:

String filename = xmlfile;
SAXBuilder builder = new SAXBuilder();
Document doc = File(filename));

XPath oPath = XPath.newInstance("//Location");
List lPath = oPath.selectNodes(doc);
System.out.println("XML has "+ lPath.size() + " locations");
// why can lPath call size()? Who's overridding it?

Iterator i = lPath.iterator();
while(i.hasNext()) {
Element eLocation = (Element);
// same question with hasNext() and next()!

Thanks in advance!

(years later I'll be laughing at this, I know!)
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find out the actual name of the concrete class by calling getClass().getName() on an instance of the class. But in general, it doesn't really matter. The purpose of the interface is to abstract away the need to depend on particular concrete classes, your code only depends on the interface that that class implements. The actual concrete class could some well-known public class, a private class, or even an anonymous class. It doesn't really matter, You know what methods you can call because you know what interface the object implements.
Jesper de Jong
Java Cowboy
Posts: 15860
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jodeci, welcome to JavaRanch.

Please check your private messages for an administrative matter. You can see them by clicking My Private Messages.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!