-
Book Overview & Buying
-
Table Of Contents
OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808
By :
You already saw that there are four access modifiers: public, private, protected, and default access. We are going to discuss them in order from most restrictive to least restrictive:
private: Only accessible within the same classprivate and other classes in the same packageprotected: default access and child classespublic: protected and classes in the other packagesPrivate access is easy. Only code in the same class can call private methods or access private fields.
Before we start, take a look at Figure 4.2. It shows the classes we'll use to explore private and default access. The big boxes are the names of the packages. The smaller boxes inside them are the classes in each package. You can refer back to this figure if you want to quickly see how the classes relate.
Figure 4.2 Classes used to show private and default access
This is perfectly legal code because everything is one class:
1: package...
Change the font size
Change margin width
Change background colour