*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""cannot be applied to" error using XML" Watch ""cannot be applied to" error using XML" New topic
Author

"cannot be applied to" error using XML

Tom Purl
Ranch Hand

Joined: May 24, 2002
Posts: 104
I wrote a small class that parses an XML file a while ago. In the past, I've compiled this class; I even have a compiled version of it still on my hard drive that works perfectly.
I came across this class again, made a few changes and tried to compile it again. I used a identical compiler (1.3.0_02) in both cases, and my CLASSPATH variable hasn't changed. However, I got a strange compilation error this time. Here's the code:

So basically, I'm passing a Document interface as a Node; I'm getting the following error:

[ removed the tab characters -ds ]
So it's telling me that I can't pass a Document as a Node. Why not? The Document interface is a subinterface of the Node interface. Shouldn't I be able to use the Document interface polymorphically?
Another strange thing. I modeled my code after an example I found at the
Crimson website. I also tried to compile the example that I got from them and got the same compilation errors! Once again, this compiled in the past.
I don't think I've screwed up my CLASSPATH for the following reason. I also have jython installed on my machine, and I'm able to import references to the org.w3c.dom package. If my CLASSPATH was hosed I wouldn't be able to do this.
Thanks in advance for any help!
[ July 29, 2002: Message edited by: Tom Purl ]
[ July 29, 2002: Message edited by: Tom Purl ]
[ July 29, 2002: Message edited by: Tom Purl ]
[ July 29, 2002: Message edited by: Dirk Schreckmann ]

Tom Purl<br />SCJP 1.4
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Let's consider another example that uses a similar structure. Does the following compile on your system?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Tom Purl
Ranch Hand

Joined: May 24, 2002
Posts: 104
Thanks for the help Dirk. I think I need to take a step back though.
In my code (and in a lot of Java example code that I've found on the web related the the XML api's) I seem to be instantiating an interface (org.w3c.dom.Document). I do so by calling the parse() method of the DocumentBuilder class. How is this possible? Every Java reference that I've ever seen says you can't instantiate an interface?
If it is indeed kosher to instantiate an interface (which it certainly seems to be based on the object returned by the DocumentBuilder.parse() method), then why can't I use it polymorphically? Why can't I say:
Node doc = new Document();
...if Document is a subinterface of the Node interface?
Thanks again for all of your help!
[ July 30, 2002: Message edited by: Tom Purl ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
The example I posted above was to ascertain if you were having a problem with your compiler specific to such a structure.
-------------------
In my code (and in a lot of Java example code that I've found on the web related the the XML api's) I seem to be instantiating an interface (org.w3c.dom.Document). I do so by calling the parse() method of the DocumentBuilder class. How is this possible?
That method must be returning an object of the interface type. That object must also be of another type that can be instantiated. You cannot instantiate an interface as you would a class - new Interface() .
Consider:
class Whatever implements SomeInterface
{
public Whatever() {}
}
You can create an object of type Whatever referred to by an identifier of type Whatever:
Whatever thing = new Whatever() ;
And you can create an object of type Whatever referred to by an identifier of the interface type because things of type Whatever are also of type SomeInterface:
SomeInterface otherThing = new Whatever() ;
This doesn't construct an interface, it constructs an object of some class that implements the interface.
------------------------
If it is indeed kosher to instantiate an interface (which it certainly seems to be based on the object returned by the DocumentBuilder.parse() method), then why can't I use it polymorphically? Why can't I say:
Node doc = new Document();
...if Document is a subinterface of the Node interface?

If Document is an interface, then you cannot instantiate it using the new operator. You can instantiate a class that implements the Document interface. Objects of that class type would also be of the super interface type.
Consider:
interface Node{}
interface Document extends Node{}
class Whatever implements Document{}
Whatever thing = new Whatever() ;
thing is of type Whatever. thing is of type Document. thing is of type Node.
thing could be used anywhere an object of type Whatever or of type Document or of type Node were required.
[ July 30, 2002: Message edited by: Dirk Schreckmann ]
Tom Purl
Ranch Hand

Joined: May 24, 2002
Posts: 104
Dirk, I've been too busy to reply so far today but the information that you gave me was great! This is exactly what I was looking for. Also, thanks for being so thorough with your explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "cannot be applied to" error using XML