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
Carrier
andDestroyer
classes into aShip
super class.Inherit from the
Ship
class for bothCarrier
andDestroyer
, and therefore save a lot of code maintenance.Use polymorphism to adapt the
serviceShip
function in theShipyard
class so that it takesShip
as 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 than...