Interfaces

Interfaces, like abstract classes and methods, provide templates of behavior that other classes are expected to implement. Interfaces, however, provide far more functionality to Java and to class and object design than do simple abstract classes and methods.

The Problem of Single Inheritance

After some deeper thought or more complex design experience, however, you might discover that the pure simplicity of the class hierarchy is restrictive, particularly when you have some behavior that needs to be used by classes in different branches of the same tree.

Look at an example that will make the problems clearer. Assume that you have a biological hierarchy with Animal at the top, and the classes Mammal and Bird underneath. Things that define ...

Get Sams Teach Yourself Java 2 in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.