I got开发者_开发技巧 this question for homework, and I'm not sure how to answer it. Could you help me out?
For any kind of "why don't..." question, it's good to think about "what would happen if...". In this case: What would happen if every Java class needed to have an interface? How would it change the existing APIs? How would it change things for people creating new classes? Any problems you find in answering these other questions could be answers to your original question.
Perhaps the question aims at the difference between the general concept of an interface to a class or a module and the language construct of a Java Interface (in the sense that all classes do have interfaces, but don't implement an Interface).
We're not here to do your homework. But if you had answered the previous question - as to what interfaces are for - you might answer this question easily.
Answer your teacher that it is because it is pointless every class to have interface. If he asks you "why?", answer him: imagine what would it be if every single man on earth had to have the skills to develop software, the strength to climb mountain peaks, tehnique to run ski, have a solid opinions in political discussions and be able to draw like Picasso... aah.. :) not to mention to speak EVERY LANGUAGE ON EARTH.
I bet there will be people who will argue with me about what's wrong in every person on earth having all those abilities, but that will render our answers off-topic. So, better not argue. At least not here :)
That will not make any sense. What will be the interface for String
or Date
?
Another way of understanding this question is to look at instances/situations when Interfaces are used. And, ask yourself what repercussions would implementing an Interface has? Consider "Creating Threads" as an example. You can create it by extending the Thread class or by implementing the Runnable interface. Interfaces, in this case, solve the problem of multiple inheritance. What problems would implementing an interface "always" solve/create?
精彩评论