Java access modifiers

By | 2015-09-28T17:41:59+05:30 May 4th, 2015|OOPS|

 

Java access modifiers determines a particular class can access the data members or methods. The “access” is specifies the scope of a class or variable or a method.

In Java, we have 4 types of access modifiers.

  1. public,
  2. default,
  3. private,
  4. protected.

public Access Modifier

public is an access modifier, If we declare a member or method as a public, those can be accessed from any were in the application (out side from the any package).

Example :


/**
*
* @author chandrashekhar
*/
public class Sample {

    public static void main(String[] args) {
        System.out.println("A : " + new A().valueA());
    }
}

class A {

    private int a = 10;

    public int valueA() {
        a += 10;
        return a;
    }
}

output :

A : 20

Class Level Visibility

If we declare a member as public then we can access that member from any ware from the application but the corresponding class should be visible. i.e before checking the member visibility we have to check class visibility.

Ex:


package pack1;
class A {

    public void m1() {
        System.out.println("Method 1");
    }
}

package pack2;
import pack1.A;
class B {

    public static void main(String args[]) {
        A a = new A();
        a.m1();  // compilation fails.
    }
}

In the above example, m1() method is public, but we can’t access m1() from out side of pack1 because the corresponding class A is not declared as public. If both are public we can access the method m1().

default Access Modifier

If a member declared as the default, then we can access that member only with in the current package and we can’t access from out side of the package. Hence default access is also known as package level access. If we didn’t mention any access for class, methods or variables by default the access modifier is “default”.

Ex:


package access;

/**
*
* @author chandrashekhar
*/
public class Sample {

    int a = 10;

    public static void main(String[] args) {
        System.out.println("A : " + new A().valueA());
    }

}
package access;
class A {

    int a = 10;

    int valueA() {
        a += 10;
        return a;
    }
}

package access2;

/**
*
* @author chandrashekhar
*/

public class Sample {

    public static void main(String[] args) {

        access.Sample sample = new access.Sample();
        sample.valueA();    // Compilation Error

    }
}

On the above example Line number 39 generates Compilation Error, because we can’t access the default methods from out side the package.

private Access Modifier

If a member declared as private then we can access that member only with in the current class.


/**
*
* @author chandrashekhar
*/
public class Sample {

    int a = 10;

    public static void main(String[] args) {
        System.out.println("Value : " + new A().valueA());
        System.out.println("A : " + new A().a);   // Compilation Error
    }

}

class A {

    private int a = 10;

    int valueA() {
        a += 10;
        return a;
    }
}

Note:

abstract methods should be visible in the child classes to provide implementation. Where as private methods are not visible in child class. Hence private-abstract combination is illegal for methods.

protected Access Modifier

Protected is the most miss understood modifier in Java. If a member declared as protected then we can access that member with in the current package, any ware but out side package only in case of child classes.
With in the current package we can access protected members either by parent reference or by child reference.
But from out side the package we can access the protected members only by using the child reference. If we are trying to use parent reference we will get Compilation Error.

Ex:

package pack1;
public class A {

    protected void m() {
        System.out.println("Protected method");
    }
}

public class B extends A {

    public static void main(String args[]) {
        A a = new A();
        a.m();
        B b = new B();
        b.m();
        A a = new B();
        a.m();
    }
}

public pack2;
import pack1.A;

public class C extends A {

    public static void main(string args[]) {
        A a = new A();
        a.m();    // Compilation Error
        C c = new C();
        c.m();
        A a = new C();
        a.m();    // Compilation Error.
    }
}

Note:   The most restricted modifier is private.
The most accessible modifier is public.
The recommended modifier for variables is private.
The recommended modifier for methods is public.

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment