Access modifiers


Access modifiers in java:-
Access modifiers  decide that whether the class 
,constructor,member or method will be accessible or not.simply access modifier define the accessibility.

There are four types of modifier in java

  1.   Access modifier   (   private,   default,  protected and public 
  2. Non-access modifier.  (  static, abstract, synchronized, native, volatile, transient etc.)

Now we will discuses access modifier only.

for your understanding I divide access modifier into to type

a. class level modifier <class level >
b. member level modifier.<member level>


a.class level modifier
 -In class level Public and default modifiers are used.
-In class only public modifier are written by-default  default are applied.

Public access modifier

if we use public key word with class it will be accessible in every where.It has highly scope among all modifier.

example

//package 1
package accessModifiers;
public class Class1 {
public void display()
{
System.out.println("I'm class1 method");
}

}  
//package2
package accessModifiers2;
import accessModifiers.*;
 class Class2 extends Class1 {
public static void main(String args[])
{
Class1 c=new Class1();
c.display();
}

}
output
I'm class1 method


default access modifier

  -if we don't use public default are applied by-default. 
-it is also called package-private because if the class has default keyword it has private for other package.

//package 1
package accessModifiers;
public class Class1 {
public void display()
{
System.out.println("I'm class1 method");
}

}  
//package2
package accessModifiers2;
import accessModifiers.*;
 class Class2 extends Class1 {
public static void main(String args[])
{
Class1 c=new Class1();
c.display();
}
}
output


b. member level modifier.<member level>

public access modifier 

-If public keyword are use method will be called
will be called from anywhere.
can access in same class.
can access in inherit class.
can access other package.

example

//package 1
package accessModifiers;
public class Class1 {
public void display()
{
System.out.println("I'm class1 method");
}

}  
//package2
package accessModifiers2;
import accessModifiers.*;
 class Class2 extends Class1 {
public static void main(String args[])
{
Class1 c=new Class1();
c.display();
}

}
output
I'm class1 method



default access modifier

-if we don't use any modifier default are used.

default access modifier method and variable are access only in same class.




protected access modifier

-protected access modifier are use the method.
  • can access in same package.
  • can access in other package in inheritance only.

//package 1

package accessModifiers;
public class Class1 {
protected void display()
{
System.out.println("I'm class1 method");
}

}  
//package2
package accessModifiers2;
import accessModifiers.*;
 class Class2 extends Class1 {
public static void main(String args[])
{
Class1 c=new Class1();
c.display();
}

}

output 
error

private protected access modifier

if private protected access modifier isuse with method or member variabel they will be only access in inheritance classes no issue if they are of same package or different.

private access modifier

private provide highest level protection.
-it can access only in it own class.
//package1

package accessModifiers;

public class Class1 {
private void display()
{
System.out.println("I'm class1 method");
}

}
//package2
package accessModifiers2;
import accessModifiers.*;
 class Class2 extends Class1 {
public static void main(String args[])
{
Class1 c=new Class1();
c.display();
}

}

output



Comments

Popular posts from this blog

java font example

File handling in java