2 min read

Java - Access modifiers

Java - Access modifiers
#java #private #final

A private anything should never go public.
It must be protected at all costs!


Let's go through all access modifiers we can encounter and use in Java.

  • Top-level

    • public : object is visible to all classes everywhere wether they are in the same package or have imported the package containing the public class.
    public class Class1 {
    • package-private: only available in its own package and it’s specified by not specifying (i.e. it’s the default if you do not specify public) there is no package-private keyword.
    class Class2 {
  • Member level

    • public: same as top-level (visible from everywhere)
    class Class1 {
        public String field1;
    • package-private i.e no modifier: same as top-level ( visible only within its own package)
    class Class1 {
        String field1;
    • protected: anywhere in its package (like package-private) but also in subclasses (even if they are declared in a different package).
    class Class1 {
        protected String field1;
    • private: only visible within the class it's declared. It's not visible anywhere else (including subclasses of it).
    class Class1 {
        private String field1;

Order of the access modifiers from least restrictive to most restrictive:

public > package-private > protected > private

Interface case

Say we have an interface like the one below. Which would be the visibility of it and its fields?

interface ServiceInterface {
    int field = 123;
    public void method1();
    void method2();
  • ServiceInterface is package private as the public modifier is missing at top level.
  • field is public and also static and final (the only fields that can be declared in an interface are constants)
  • method1() is public as the public modifier is present
  • method2() is also public as every method inside an interface


The final keyword

We use final for:

  • an attribute of a class that we don't want to be changed during the execution. This field can be assigned either in the constructor of the class or when first declared.
    public class A {            |      public class A {
         public final int a;    |           public final int a = 10;
                                |      }
         public A() {           |      
         this.a = 10;           |        
   }                            |      
  • not letting a method be overridden.
public class A {                 |     public class B extends A {
     public void method1() {     |          @Override
     // implementation           |          public void method1() {
     }                           |             // other implementation
}                                |          }
                                 |     }

By setting the method1 as final, subclasses won't be able to override it anymore (will get a compile-time error).

Don't miss out on more posts like this! Susbcribe to our free newsletter!
Currently I am working on a Java Interview e-book designed to successfully get you through any Java technical interview you may take.
Stay tuned! 🚀