0

I believe the answer to my question is a huge NO, you can't do something like

public interface Parser {
...
}

and in another class

Parser parser = new Parser();

Of course the Parser parser part is perfectly fine, but new Parser() should very obviously be wrong, not allowed, give a compiler error. This answer here at SO and lots of documentation I don't think is necessary to quote says so.

I am still asking this question because I'm quoting line 122 of android.text.Html class, where my Eclipse IDE tells me this Parser whose constructor with no arguments I'm calling belongs to the interface org.xml.sax.Parser, as shown in this screenshot:

enter image description here

Also, the type of the reference is that same Parser class.

What am I looking at? Is Eclipse pointing to the wrong .class file and this is actually a class also called Parser? I suspect it is, but if so, how does Eclipse (not) know which class the reference is a type of and the constructor belongs to? Could something like this make the compiler link the wrong classes, if it makes the exact mistake that is (I think) being made here by the module that knows where to take me when I hold down Control and left click on a method?

Community
  • 1
  • 1
Blueriver
  • 3,212
  • 3
  • 16
  • 33
  • 1
    Check the import statement in the HTML class link you have pasted. Its `import org.ccil.cowan.tagsoup.Parser;` not org.xml.sax. – Codebender Jul 20 '15 at 08:25

2 Answers2

1

Is Eclipse pointing to the wrong .class file?

No.

and this is actually a class also called Parser?

Yes.

I suspect it is, but if so, how does Eclipse (not) know which class the reference is a type of and the constructor belongs to?

Based on the import statement on the top of Java file.

Basically you need to learn about how exactly import works.

Using Package Members

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Line 22 is `import org.ccil.cowan.tagsoup.Parser;`. It makes sense that this is the `Parser` class of my screenshot, since you couldn't use a different class with the same unqualified name. But then why is Eclipse pointing me to the org.xml.sax.Parser interface? It is not even in the same package as the Html class (though the Html class does import other classes from package org.xml.sax) – Blueriver Jul 20 '15 at 08:30
  • @Blueriver While you developing the code, eclipse gives you the suggestions. Based on the import you did, it shows. – Suresh Atta Jul 20 '15 at 08:32
  • I see. That changes things a bit for me, I always thought what Eclipse showed me was what was getting linked when compiling. Thank you! – Blueriver Jul 20 '15 at 09:01
  • @Blueriver, you were right in assuming that Eclipse shows the javadoc of the resolved element as seen by the compiler. This means, the javadoc hover in your picture indeed could indicate a bug in Eclipse. Can you reproduce this effect, ideally in a small example project? – Stephan Herrmann Jul 20 '15 at 20:21
0

You can perform new MyInterface() {};

MyInterface myInterface= new MyInterface() {
                @Override
                public int myMethod() {
                    return 1;
                }
            }

It creates an anonymous inner class. However, without the implementation specified within {}, no you can't.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • 2
    The OP doesn't have `{}` :) . Either `Parser` is class which implements the interface `...Parser` OR eclipse has gone crazy – TheLostMind Jul 20 '15 at 08:22
  • 2
    I don't think, this answers the question. – Suresh Atta Jul 20 '15 at 08:23
  • Sorry, I don't see how inner classes are involved in this matter. Though I don't know many things about the inner workings of Java. Could you please elaborate a little bit? – Blueriver Jul 20 '15 at 08:25