Access Control in Java

How a member can be accessed is determine by the access specifier that modifies its declaration.
Java's Access Specifiers are

  1. public
  2. private
  3. protected
  4. default
If no access specifies is used then by default the member of a class is public within its own package but cannot be accessed outside of its package.


Some difference Between Access Control:


1.Public:


The public Access specifies are  visible in full project or code.

Declaration:

                 
             public int a;

2. Private:

 

               The private Access specifies are  visible only in class you can not access these specifies outside the class or method if declare in method.


Declaration:

                            private int a;


3.Protected:

                         
                        The protected Access specifies are  visible to package and all subclasses you use in project or code. This modifier is also accessible in outside but only inheritance. 

Declaration:


                             Declaration of this modifier is little different look like this

First declare Package:


  package p;

class A{

protected void Text(){System.out.println("Hello")}

}

import p.*;

class B extend A{

public static void main (strings args[]){
 B b = new B();
b.Text();

}
}




4.Default:


The default Access specifies are if you use no Access specifies then Java treat as a default Access specifics and visible to package


Declaration:

   
                      int a:  // This is default Access specifies or also called default modifier. 





Comments

Popular posts from this blog

Inheritance in Java

Operators in Java Language?

Type Casting or Conversion in Java