
Android Programming with Kotlin for Beginners
By :

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
and Destroyer
classes into a Ship
super class.
Inherit from the Ship
class for both Carrier
and Destroyer
, and therefore save a lot of code maintenance.
Use polymorphism to adapt the serviceShip
function in the Shipyard
class so that it takes Ship
as a parameter, and can therefore service any instance that inherits from Ship
, 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...