Member Access and Inheritance

As you learned in Chapter 6, often an instance variable of a class will be declared private to prevent its unauthorized use or tampering. Inheriting a class does not overrule the private access restriction. Thus, even though a subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared private. For example, if, as shown here, width and height are made private in TwoDShape, then Triangle will not be able to access them:

Image

The Triangle class will not compile because the reference to width and height inside the area( ) method causes an access violation. ...

Get Java, A Beginner's Guide, 5th Edition, 5th Edition 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.