Chapter 8. Serious Polymorphism: Interfaces and Abstract Classes
Inheritance is just the beginning. To exploit polymorphism, we need interfaces (and not the GUI kind). We need to go beyond simple inheritance to a level of flexibility and extensibility you can get only by designing and coding to interface specifications. Some of the coolest parts of Java wouldnât even be possible without interfaces, so even if you donât design with them yourself, you still have to use them. But youâll want to design with them. Youâll need to design with them. Youâll wonder how you ever lived without them. Whatâs an interface? Itâs a 100% abstract class. Whatâs an abstract class? Itâs a class that canât be instantiated. Whatâs that good for? Youâll see in just a few moments. But if you think about the end of the previous chapter, and how we used polymorphic arguments so that a single Vet method could take Animal subclasses of all types, well, that was just scratching the surface. Interfaces are the poly in polymorphism. The ab in abstract. The caffeine in Java.
Did we forget about something when we designed this?
The class structure isnât too bad. Weâve designed it so that duplicate code is kept to a minimum, and weâve overridden the methods that we think should ...
Get Head First Java, 3rd 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.