22.8. Parent and child class data

A parent class is a subset of a child class; that is, the child class includes all of the data members and methods as the parent. It’s common also to speak of a parent class as a base class, and to speak of a child class as a derived class.

In order to allow the child class to have access to the private fields and methods of the parent class we need to declare those fields to be protected rather than private.

What about access to the public fields of the parent? The child can always access these, but these fields do not necessarily have to be public members of the child as well. A child can change a parent method from protected to public or from public to protected by redeclaring it.

In the declaration of a ...

Get Software Engineering and Computer Games now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.