Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Saturday 4 March 2017

Access Modifiers in java

 

Java Access Modifiers

The access modifiers in java specifies accessibility of class, data members, methods, and constructor.

Java access modifiers are those which are applied before class, data member, method, constructors. Access Modifiers are used to where to access and where not to access classes, data members, method(functions), constructor.


Access Modifiers In Java




There are 4 types of java access modifiers

  1. Private
  2. Default (not a keyword)
  3. Protected
  4. Public

Private Access Modifier

Private data member and private method of class is accessible only within class not outside of another class.

For example:

class Hello
{
private int a = 10;
private void show()
{
System.out.println("private method");
}
}
class AccessPrivateMembers
{
public static void main(String args[])
{
Hello h = new Hello();
System.out.println(h.a);//compile time error, can't access private data
h.show();//compile time error, can't access private method
}
}

In above example, there are two classes Hello with private data members and methods and second class is AccessPrivateMembers where we try to accessing private members but it will throw compile time error.

👉 : A class cannot be a private or protected except nested class.

For example:

private class Hello
{
--------
--------
--------
}

protected class Demo
{
--------
--------
--------
}


Constructor with private access modifier

For example:

class Hello
{
private Hello()//private constructor
{
System.out.println("private constructor");
}
}
class Test
{
public static void main(String args[])
{
Hello h = new Hello();//compile time error
}
}

output: compile time error

In this above example, if you make any constructor private, you can't create the object of that class from outside the class.

👉 : We cannot use access modifiers with local variables.


Default Access Modifier

When no access modifier is specified for class , data member and method, it is known as default access modifier by default.

Default members(class, data members, methods) are accessible only within the same class and another class of the same package.

For example:

//save by A.java
package pack;
class A
{
void show()
{
System.out.println("hello java");
}
}

//save by B.java
package pack1;
import pack;
class B
{
public static void main(String args[])
{
A a = new A();//can't access outside of package
a.show();//can't access outside of package
}
}

In this above example, we cannot access class A and its method show() because there is default access modifier i.e default members can't be access  another packages.


Protected Access Modifier

The protected access modifier is accessible within the same class and another class of same package and outside of a package also but through inheritance only.

Protected access modifier can be applied on the data member, methods and constructor but it cannot be applied on the class and interface.

In below example, We have created two packages packfirst and packsecond. In packfirst, class A is public so we can access this class outside of packfirst but method display() is declared as protected so it is only accessible outside of a package of packfirst through inheritance only.

For example:

//save by A.java
package packfirst;
public class A
{
protected void display()
{
System.out.println("this is protected method");
}
}

//save by B.java
package packsecond;
import packfirst;
class B extends A
{
public static void main(String args[])
{
B b = new B();
b.display();
}
}

output: this is protected method


Public Access Modifier

The public access modifier is accessible everywhere i.e in the same class and outside of class, in the same package and outside of the package.

public access modifier can be applied on the class, data members, methods.

For example1:

class Hello
{
public int a = 20;
public void show()
{
System.out.println("Java Programming");
}
}
public class Demo
{
public static void main(String args[])
{
Hello h = new Hello();
System.out.println(h.a);
h.show();
}
}

save it by Demo.java//because Demo class is declared as public

output: 20
            Java Programming

For example2:


//save it by A.java
package pack;
public class A
{
public void show()
{
System.out.println("hello java");
}
}

//save it by B.java

package mypack;
import pack;
class B
{
public static void main(String args[])
{
A a = new A();
a.show();
}
}

output: hello java


Share:

3 comments:

  1. Very nice information. Thanks For sharing good article.I have written java8 blog visit Java8 Tutorials

    ReplyDelete
  2. Thanks for shared wonderful information of giving best information.its more useful and more helpful. great doing keep sharing: Visit: https://nearlearn.com/machine-learning-classroom-training-in-bangalore-india

    Best Machine Learning Course in Bangalore

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate