Sean Nguyen
Sean Nguyen

Reputation: 13128

Compiler error: Type mismatch when assigned interface variable to a class

I have a strange problem i don't know if i miss something. Here is my code

public interface Book{
}

public class MyBook implements Book
{
}

public static void main(String[] args)
{
   Book b = new MyBook();  // compiler error: Type mismatch ....
} 

Can somebody explains to me is this really a compiler error or just my eclipse is acting weird?

Thanks,

Upvotes: 1

Views: 711

Answers (3)

Sean Nguyen
Sean Nguyen

Reputation: 13128

Sorry, there is another interface of exact same name that is in the imported statement that is causing the problem. Thanks.

Upvotes: 0

wjans
wjans

Reputation: 10115

Your main method is not in a class, try putting it inside a class.

Also make sure to have only one public class per Java file.

Upvotes: 3

SMK
SMK

Reputation: 2158

I think after implementation of interface you can make object of class that implemented interface so make the object of class "MyBook"

Upvotes: 0

Related Questions