Q: Wait a minute, interfaces don’t really give you multiple inheritance, because you can’t put any implementation code in them. If all the methods are abstract, what does an interface really buy you?
A:
A: Polymorphism, polymorphism, polymorphism. Interfaces are the ultimate in flexibility, because if you use interfaces instead of concrete subclasses (or even abstract superclass types) as arguments and return types, you can pass anything that implements that interface. And think about it—with an interface, a class doesn’t have to come from just one inheritance tree. A class can extend one class, and implement an interface. But another class might implement the same interface, yet come from a completely different inheritance tree! So you get to treat an object by the role it plays, rather than by the class type from which it was instantiated.
You are currently reading a PREVIEW of this book.
Get instant access to over
$1 million worth of books and videos.