• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interface method confusion

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 = builder.build(new 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) i.next();
....
}
// same question with hasNext() and next()!



Thanks in advance!

(years later I'll be laughing at this, I know!)
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic