Java Reference
In-Depth Information
The Triangle class will not compile because the reference to width and height inside the
area( ) method causes an access violation. Since width and height are declared private ,
they are accessible only by other members of their own class. Subclasses have no access to
them.
Remember that a class member that has been declared private will remain private to its
class. It is not accessible by any code outside its class, including subclasses.
At first, you might think that the fact that subclasses do not have access to the private
members of superclasses is a serious restriction that would prevent the use of private mem-
bers in many situations. However, this is not true. As explained in Chapter 6 , Java program-
mers typically use accessor methods to provide access to the private members of a class.
Search WWH ::




Custom Search