Member Access and Inheritance
Although a subclass includes all of the members of its super class, it cannot access those members of the super class that have been declared as private. For example, consider the following simple class hierarchy.
This program will not compile because the reference to method of B causes an access violation, Since j is declared as private, it is only accessible by other members of its own class. Sub classes have no access to it.
A ‘More Practical Example
Let’s look at a more practical example that will help illustrate the power of inheritance. Here, the final version of the Box class developed in the preceding chapter will be extended to include a for component called weight. Thus, the new class will contain a box’s width, height, depth, and weight .
Box Weight inherits all of the characteristics of Box and adds to them the weight component. It is not necessary for Box Weight to re-create all of the features found in Box. It can simply extend B~ to meet its own purposes. A major advantage of is that once you have created a super class that defines the attributes common to a set of objects, it can be used to create any number of more specific sub classes. Each subclass can precisely tailor its own classification. For. example, the.following class inherits Box and adds a color attribute:
Remember, once you have created a super class that defines the general aspects of an object, that super class can be inherited to form specialized classes. Each subclass simply adds its own, unique attributes. This is the essence of inheritance.