Classes using the Inheritance example app
We have looked at the way we can create hierarchies of classes to model the system that fits our app. So, let's build a project to improve upon the naval battle we had in the previous chapter.
Create a new project called Basic Classes with Inheritance Example using the Empty Activity template. As you have come to expect, the completed code can be found in the Chapter11 folder.
This is what we are going to do:
- Put most of the functionality of the
CarrierandDestroyerclasses into aShipsuper class. - Inherit from the
Shipclass for bothCarrierandDestroyer, and therefore save a lot of code maintenance. - Use polymorphism to adapt the
serviceShipfunction in theShipyardclass so that it takesShipas a parameter, and can therefore service any instance that inherits fromShip, thereby reducing the number of functions in the class. - We will also see that not only is there less code achieving the same functionality as before, but it is more encapsulated...