Manager
extends Driver
, therefore if you construct an ArrayList<Driver>
you can store Manager
objects in it as well and they will behave like Manager
, where applicable.
This is called polymorphism and it allows you to define different behaviour for various types of objects and then make use of it, without knowing, what kind of object will you receive in runtime.
Therefore, you can define different .showMenu()
methods for Driver
and for Manager
and when you get an object user
of Driver
class and call user.showMenu()
, an appropriate version of method will run, depending on whether user
is of Manager
or Driver
class.
EDIT :
As to the second part of your question, basically yes, if you have the same method defined in Driver
and Manager
classes, an appropriate version of it will be called, depending on the class of your object. If however you have it defined only in Driver
class, the same version of method will be used both for Driver
and Manager
objects.
If you have a method defined only in Manager
, but not in Driver
, you won't be able to call such method for Driver
type variables, regardless of what type of object will they hold in runtime, because you can never be sure, that the variable will always contain Manager
object.