Content deleted Content added
No edit summary |
Format code |
||
Line 11:
For example:
<source lang="Java">
</source>
An interface:
* declares only method headers and public constants.
* cannot be instantiated.
* can be implemented by a class.
* cannot extend a class.
* can extend several other interfaces.
==Usage==
Line 44:
<source lang="java">
public interface Predator {
}
</source>
Line 60:
public class Lion implements Predator {
}
</source>
Line 75:
Classes can implement multiple interfaces:
<source lang="Java">
</source>
Interfaces can share common class methods:
<source lang="Java">
class Animal implements LikesFood, LikesWater {
</source>
Line 88:
<source lang="Java">
class Animal implements Shares<Boolean>, Shares<Integer> ...
//
</source>
Line 96:
Interfaces can extend several other interfaces, using the same formula as described below. For example,
<source lang="java">
</source>
is legal and defines a subinterface. Note how it allows multiple inheritance, unlike classes. Note also that <code>Predator</code> and <code>Venomous</code> may possibly define or inherit methods with the same signature, say <code>kill(Prey p)</code>. When a class implements <code>VenomousPredator</code> it will implement both methods simultaneously.
|