GuilleOjeda
GuilleOjeda

Reputation: 3361

Can an interface be instantiated, or is Eclipse pointing to the wrong .class file?

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?

Upvotes: 0

Views: 101

Answers (2)

NimChimpsky
NimChimpsky

Reputation: 47290

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.

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

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

Upvotes: 1

Related Questions