Actual use of interface in java [duplicate]
Possible Duplicate:
Java Interfaces?
What is the use of interface in j开发者_如何转开发ava? Is interface helps to provide multiple inheritance actually?
Interfaces are used as a contract.
If an Object implements an interface, it is guaranteed to offer the functionality specified in the interface.
Interfaces also allow you to treat Objects (that implement the interface) polymorphicly.
No, multiple inheritance is when you extend behaviours from more than an object.
Interfaces provide a safe way to declare what an object is able to do, not how it does it. That's why you can implement multiple interfaces.
They are used in many kinds of problems, for example when you want to give to your object the same capability that is orthogonal to what these object actually are: for example the interface Comparable
states that object can be compared with other objects of the same type. This doesn't relate with what kind of object it is at all.
精彩评论