Derived Class Objects

Contact Us or call 1-877-932-8228
Derived Class Objects

Derived Class Objects

You can view a derived class object as having a complete base class object inside it. Let's assume that the Entity class defines the fields name, energy, and position, and methods moveTo() and changeEnergy().

The Playable class adds a field playerID, and the Wizard class adds a spells field (an array of spells they can cast) and a castSpell() method.

Inheritance as Boxes Within Boxes

Any Wizard object contains all the elements inside its box, include those of the base classes. So, for example, the complete set of properties in a Wizard object is:

  • name
  • energy
  • position
  • playerID
  • spells

A Wizard reference to a Wizard object has access to any public elements from any class in the inheritance chain from Object to Wizard. Code inside the Wizard class has access to all elements of the base classes (except those defined as private in the base class - those are present, but not directly accessible).

A more complete description of access levels is coming up.

Note: although it appears that a base class object is physically located inside the derived class instance, it is not actually implemented that way.

Next