开发者

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

开发者 https://www.devze.com 2023-03-18 12:55 出处:网络
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

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 th开发者_如何学JAVAis really a compiler error or just my eclipse is acting weird?

Thanks,


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号