开发者

Actual use of interface in java [duplicate]

开发者 https://www.devze.com 2023-01-03 01:15 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Java Interfaces? What is the use of interface in j开发者_如何转开发ava? Is interface helps to provide mul
This question already has answers here: Closed 12 years ago.

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.

0

精彩评论

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