UPDATE Wilfrid Laurier University
213 FINAL EXAM REVIEW
Protected access: child classes to this class can access the method/variables even if
they aren't in the same package
**There are 2 classifications of instance variables & methods that allow them to be accessed
by name in a derived class** → one of them is protected access (always gives access)
➔ If a method/instance variable is modified by protected (rather than
public/private) → it can be accessed by name inside its own class definition
◆ it can be accessed by name inside any class derived from it
◆ it can also be accessed by name in the definition of any class in the
same package (even if the class in the same package is not derived
from it)
◆ Can be accessed in a different package if the class is derived from it
★ Example: consider the class HourlyEmployee that was derived from the base class
Employee.
If the private instance variables name & hireDate had been marked protected in the
class Employee, the definition of toString in the derived class HourlyEmployee could
be simplified to the following: (basically removed the “get”)
Pitfall: A Restriction on Protected Access ★
● A class can access its own classes’ inherited variables & methods that are marked
protected in the base class, but cannot directly access any such instance variables
● For example, n is an instance variable of the base class B & an instance variable of the
derived class D. D can access n whenever n is used as an instance variable of D, but D
cannot access n when n is used as an instance variable of B.
, ● If the classes B and D are in the same package, you will not get the error message
because, in Java, protected access implies package access.
Package/Friendly/Default access: each class that is derived from the same
package has access to this method/variables
**There are 2 classifications of instance variables & methods that allow them to be accessed
by name in a derived class** → one of them is package access (gives access if the derived class
is in the same package as the base class)
● If you do not place any of these modifiers (public, private, or protected) before
an instance variable or method definition → then the instance variable/method
can bg accgssgd bD namg insidg Ēhg dgfiniĒion of anD class in Ēhg samg
packagg buĒ noĒ ouĒsidg of Ēhg packagg → package access
○ Use package access in situations → package of cooperating classes that
act as a single encapsulated unit
○ Package access is more restricted than protected
○ Package access gives more control to the programmer defining the
classes
○ If you control the package directory (folder) → you control who is allowed
package access
Pitfall: Forgetting about the default package ★
● If a class in your current directory is not in any other package → then it is in
the default package
● If an instance variable/method has package access → then it can be accessed
by name in the definition of any other class in the default package
Public access: Any class can access this regardless of package or whatever
➔ The public access modifier is the direct opposite of the private access
modifier
◆ A class, method or variable can be declared as public → is accessible
from any class
◆ It can be accessed in its own class
◆ Can be accessed from a different class
◆ Can be accessed from a different package
, Private access: Means that only members of this class have access to this method/
variable
➔ The most restricted access level
◆ Can only be accgssgd in iĒs own class C samg packagg
➔ Class & interfaces cannot be private
➔ Variables that are declared private can be accessed outside the class → if
public getter methods are present in the class
★ ALL 4 ACCESS (private, public, package, protected)