Access/ Visibility Modifiers In Java

By YuwebDesign

  1. help to restrict the accessibility (scope) of a class,
    constructor, variable, method or data member.
  2. specify which classes can access a given class and its
    fields, constructors and methods. sometimes referred to in daily
    speech as Java access specifiers.
  3. can be specified separately for a class, its
    constructors, fields and methods.
  4. Assigning an access modifier to a class, constructor,
    field or method is also sometimes referred to as “marking it as
    modifierName”. E.g., “marking the method as public”.

  1. Try to use the most restrictive access level that makes
    sense for a particular member.
  2. Use private unless you have a good reason not to.
  3. Progress towards the public as the needs arise and design
    warrants it.
  4. Avoid public fields except for constants.
  5. When exposing members ask yourself if you are exposing
    representation choices or abstraction choices. The first is
    something you want to avoid as it will introduce too many
    dependencies on the actual representation rather than on its
    observable behavior.
  6. As a general rule, try to avoid overriding method
    implementations by subclassing; it’s too easy to screw up the
    logic. Declare abstract protected methods if you intend for it to
    be overridden.

Can be applied to
Modifiers Class Nested Class Constructor Method Field
Public Y Y Y Y Y
Default Y Y Y Y Y
Protected N Y Y Y Y
Private N Y Y Y Y
Access Level
Modifiers (Same) Class nonsubbed (Any Class Same) Package (Subclass and
(Different Package but This) Subclass World/global (Different Package
Non-subclass/ different class)
Public Y Y Y Y
Protected Y Y Y N
Default Y Y N N
Private Y N N N

Classes, fields, constructors and methods can have 1 of 4 different Java access modifiers:

  1. Default – No keyword required (package)
  2. Private
  3. Protected
  4. Public

  1. The methods or data members declared as private are accessible
    only within the class
    in which they are declared.
  2. Any other class of same package will not be able to
    access these members.
  3. Code inside subclasses cannot access the variable or
    method, nor can code from any external class.
  4. Interface can not be declared as private because private
    means “only visible within the enclosing class”.
  5. Classes cannot be marked with the private access
    modifier. “Private” in terms of application to classes, can apply
    only to nested classes and not on top level classes. Marking a
    class with the private access modifier would mean that no other
    class could access it, which means that you could not really use
    the class at all. Therefore the private access modifier is not
    allowed for classes.

E.g, assigning private access modifier to a field:

public class Clock { private long time = 0; } //member
variable time inside the Clock class //cannot be accessed from
code outside the Clock class.

  1. Fields are often declared private to control the access
    to them from the outside world.
  2. In some cases the fields are truly private, meaning they
    are only used internally in the class.
  3. In other cases the fields can be accessed via accessor
    methods (e.g. getters and setters). E.g., public
    class Clock { private long time = 0; public long getTime() {
    return this.time; } public void setTime(long theTime) {
    this.time = theTime; } } //methods getTime() and setTime() can
    access the time member variable //these methods are declared
    public, //meaning they can be called from anywhere in the

If any class constructor is assigned the private Java access modifier

  1. you cannot create the instance of that class from outside
    the class. the constructor cannot be called from anywhere outside
    the class.
  2. it can still get called from other constructors, or from
    static (no instance of a class needed) methods in the same class.
    E.g.,: public class Clock { private long time = 0;
    private Clock(long time) { this.time = time; } public Clock(long
    time, long timeOffset) { this(time); this.time += timeOffset; }
    public static Clock newClock() { return new
    Clock(System.currentTimeMillis()); } } //Clock class contains a
    private constructor and a public constructor. //private
    constructor is called from the public constructor (the statement
    this();). //private constructor is also called from the static
    method newClock(). //That is a just a demonstration (not a
    clever design in any way) //that a private constructor can be
    called from public constructors //and from static (no instance
    of a class needed) methods inside the same class.

  1. sometimes referred to as the package or package-private
    access modifier.
  2. declared by not writing any access modifier at all.
  3. code inside the class itself as well as code inside
    classes in the same package as this class, can access the class,
    field, constructor or method which the default access modifier is
    assigned to.
  4. The data members, class or methods having default access
    modifier are accessible only within the same package.
  5. Subclasses cannot access methods and member variables
    (fields) in the superclass, if these methods and fields are
    marked with the default access modifier, unless the subclass is
    located in the same package as the superclass.
  6. With packages having the default access modifiers, trying
    to access a class from one package from a class of second package
    will result in error.
  7. fields in an interface are implicitly public static final
  8. methods in an interface are by default public

Default (package) access modifier example:

public class Clock { long time = 0; } public class
ClockReader { Clock clock = new Clock(); public long readClock{
return clock.time; } } //time field in the Clock class has no
access modifier, //meaning it is implicitly assigned the
default/package access modifier. //ClockReader class can read the
time member variable of the Clock object, //provided that
ClockReader and Clock are located in the same Java package.

  1. accessible within same package or sub
    classes in different package
  2. same access as the default access modifier (same package)
    + outside the package but through inheritance only. Variables,
    methods, and constructors, which are declared protected in a
    superclass can be accessed by

    • any subclasses within the package of the protected
      members’ class
    • only by the subclasses in other package
  3. The protected access modifier cannot be applied to class
    and interfaces. Methods, fields can be declared protected,
    however methods and fields in a interface cannot be declared
  4. Protected access gives the subclass a chance to use the
    helper method or variable, while preventing a nonrelated class
    from trying to use it.

Protected access modifier example:

public class Clock { protected long time = 0; // time
in milliseconds } public class SmartClock() extends Clock{ public
long getTimeInSeconds() { return this.time / 1000; } } //subclass
SmartClock has a method called getTimeInSeconds() //which accesses
the time variable of the superclass Clock. //This is possible even
if Clock and SmartClock are not located in the same package,
//because the time field is marked with the protected Java access

  1. has the widest scope among all other access modifiers.
  2. all code can access the class, field, constructor or
    method, regardless of where the accessing code is located.
  3. The accessing code can be anywhere in a program: in a
    different class and different package. There is no restriction on
    the scope of a public data members.
  4. fields in an interface are implicitly public static final
  5. methods in an interface are by default public

Public access modifier example:

public class Clock { public long time = 0; } public
class ClockReader { Clock clock = new Clock(); public long
readClock{ return clock.time; } } //time field in the Clock class
is marked as public //ClockReader class can access the time field
in the Clock //no matter what package the ClockReader is located

  1. Java access modifier assigned to a Java class takes
    precedence over any access modifiers assigned to fields,
    constructors and methods of that class.
  2. If the class is marked with the default
    access modifier, then no other class outside the same Java
    package can access that class, including its constructors, fields
    and methods. It doesn’t help that you declare these fields
    public, or even public static.
  3. private and protected cannot be assigned
    to a class. Only to constructors, methods and fields inside
  4. Classes can only have the default
    (package) and public
    access modifier assigned to them.

  1. Java interfaces are meant to specify fields and methods
    that are publicly available in classes that implement the
    interfaces. Therefore you cannot use the private and protected
    access modifiers in interfaces.
  2. Fields and methods in interfaces are implicitly declared
    public if you leave out an access modifier, so you cannot use the
    default access modifier either (no access modifier).

  1. When you create a subclass of some class, the methods in
    the subclass cannot have less accessible access modifiers
    assigned to them than they had in the superclass.

    • if a method in the superclass is public then it must be
      public in the subclass too in case the subclass overrides the
    • If a method in the superclass is protected then it must
      be either protected or public in the subclass; it cannot be
    • Methods declared private are not inherited at all, so
      there is no rule for them.
  2. While it is not allowed to decrease accessibility of an
    overridden method, it is allowed to expand accessibility of an
    overridden method. E.g, if a method is assigned the default
    access modifier in the superclass, then it is allowed to assign
    the overridden method in the subclass the public access modifier.

Leave a Reply or Comment

Your email address will not be published. Required fields are marked *