Tuesday, February 5, 2019

Access modifier in Java


Access modifier in Java
The access modifiers in java define accessibility (scope) of variable, method, constructor or class. There are 4 types of access modifiers in java.
1.    Public
2.    Private
3.    Protected
4.    Default

You don’t have to explicitly put default modifier.JVM will use default modifier if you do not provide Public, Private and Protected.

Public access modifier:
Public modifier is accessible in whole java world. If you put class as public that means that class is available everywhere.

package org;
import com.A;
public class B {

                public static void main(String args[])
                {
                                A objA=new A(20);
                                objA.methodA();
                                System.out.println("Value of variable a is: "+objA.a);
                }
}

When you run above program, you will get below output:
In method of class A
Value of variable a is: 20
Private access modifier:
The private access modifier is accessible only within class.
You cannot use private and protected with class unless and until it is nested class.
package com;
public class A {
               
                private int a;               
                A(int a)
                {
                                this.a=a;
                }
                private void methodA()
                {
                                System.out.println("In method of class A");
                }
}
class B {

                public static void main(String args[])
                {
                                A objA=new A(20);
                                objA.methodA();
                                System.out.println("Value of variable a is: "+objA.a);
                }
}

You will get compilation error at line no.22 and 23, as you cannot access private variables or methods from outside the class
Default access modifier:
If you do not provide any access, JVM considers it as default access. In case of default access modifier, you cannot access method, variable or class outside of the package.
Create a class named A.java in package com.
package com;
public class A {
               
                public int a;
               
                public A(int a)
                {
                                this.a=a;
                }
                public void methodA()
                {
                                System.out.println("In method of class A");
                }
}
Create another class named “B.java” in package com
package org;
import com.A;
public class B {

                public static void main(String args[])
                {
                                A objA=new A(20);
                                objA.methodA();
                                System.out.println("Value of variable a is: "+objA.a);
                }
}

Here you will get compilation error at line no.11 and 12, as we are trying access variable a and method A of class A outside the package “com”.
Protected access modifier:
Protected access modifiers can be accessed within the same package or outside the package by inheritance only.
Let’s understand with the help of example:
Create a class named A.java in package com.
package com;
public class A {

                protected int a;
                protected A(int a)
                {
                                this.a=a;
                }
                protected void methodA()
                {
                                System.out.println("In method of class A");
                }
}

Create another class named “B.java” in package com.
package org;
import com.A;
public class B extends A{

                B(int a) {
                                super(a);               
                }
                public static void main(String args[])
                {
                                B ObjB=new B(20);
                                ObjB.methodA();
                                System.out.println("Value of variable a is: "+ObjB.a);
                }
}

When you run above program, you will get below output:
In method of class A

Value of variable a is: 20
As you can see, we are able to access class A’s variable a and method A using inheritance.

No comments:

Post a Comment