Exercises
With interfaces and abstract
classes, we can improve our application structure even further! Take a look at the following exercises to test your knowledge:
- Dinosaurs, no matter the exact species, have common behaviors such as eating and moving. Define an interface that encapsulates these behaviors, come up with a logical name for it, and implement it in the
Dinosaur
class. - Our park uses different types of vehicles for different purposes. Design an
abstract
class calledVehicle
and derive concrete classes such asJeep
andHelicopter
from it. - Modify the
Vehicle
class so that it includes anabstract
method calledtravel()
that provides different implementations in its subclasses. - Make our
Dinosaur
class sortable by implementing theComparable
interface to compare dinosaurs based on their age. - Similarly, our employees also have common behaviors. Define a
Worker
interface with methods that represent these behaviors and implement it in theEmployee
class...