DEFINATION : ACCESS
SPECIFIERS
Access specifiers(or access modifiers) are keywords in
object-oriented languages that sets the accessibility of the
classes , methods and others members
.
THEREARE4 TYPESOF JAVAACCESSSPECIFIERS:
i. DEFAULT
ii. PRIVATE
iii.PROTECTED
iv.PUBLIC
Access Modifiers in java
• Java provides a number of access modifiers to set
access levels for classes, variables, methods and
constructors.
The four access levels are −
• Visible to the package, the default. No modifiers are
needed.
• Visible to the class only (private).
• Visible to the world (public).
• Visible to the package and all subclasses (protected).
1.]DEFAULT ACCESS SPECIFIER:
No keyword is required to specify default
access specifier
When no access modifier is specified for a
class, method or data member it is said to be
having the default access specifier by default.
Default access specifier are accessible within the
same package.
2. ]PRIVATE ACCESS SPECIFIER:
 The private access specifier is specified using the
keyword private.
 The methods or data members declared as private
are accessible only within the class in which they are
declared.
Any other class of same package will not be able to
access these members.
 Classes or interface can not be declared as private.
PRIVATE Example
1.class A{
2.private int data=40;
3.private void msg(){System.out.println("Hello java");}
4.}
5.
6.public class Simple{
7. public static void main(String args[]){
8. A obj=new A();
9. System.out.println(obj.data);//Compile Time Error
10. obj.msg();//Compile Time Error
11. }
12.}
PRIVATE Example – setter & getter
public class Logger {
private String format;
public String getFormat() {
return this.format;
}
public void setFormat(String format) {
this.format = format;
}
}
PRIVATE Example – setter & getter
class Data {
private String name;
// getter method
public String getName() {
return this.name;
}
// setter method
public void setName(String name) {
this.name= name;
}
}
public class Main {
public static void main(String[] main){
Data d = new Data();
// access the private variable using the getter and setter
d.setName(“Ganpat Univ");
System.out.println(d.getName());
}
}
3. ]PROTECTED ACCESS
SPECIFIER:
The protected access specifier is specified
using the keyword protected.
The methods or data members declared as
protected are accessible within same package
or sub classes in different package.
PROTECTED Example
class Animal {
// protected method
protected void display() {
System.out.println("I am an animal");
}
}
class Dog extends Animal {
public static void main(String[] args) {
// create an object of Dog class
Dog d = new Dog();
// access protected method
d.display();
}
}
PROTECTED Example
package p1;
//Class A
public class A
{
protected void display()
{
System.out.println("Learning JAVA");
}
}
package p2;
import p1.*; //importing all classes in package p1
//Class B is subclass of A
class B extends A
{
public static void main(String args[])
{
B obj = new B();
obj.display();
}
}
.4]PUBLIC ACCESS SPECIFIER:
 The public access specifier is specified using the
keyword public.
 The public access specifier has the widest scope
among all other access modifiers.
 Classes, methods or data members which are
declared as public are accessible from every where in
the program. There is no restriction on the scope of
a public data members.
ACCESS LEVELS
Specifier Class Package Subclass Everywhere
Default Y Y N N
Private Y N N N
Protected Y Y Y N
Public Y Y Y Y
Access Modifiers

Access modifiers in java

  • 1.
    DEFINATION : ACCESS SPECIFIERS Accessspecifiers(or access modifiers) are keywords in object-oriented languages that sets the accessibility of the classes , methods and others members
  • 2.
    . THEREARE4 TYPESOF JAVAACCESSSPECIFIERS: i.DEFAULT ii. PRIVATE iii.PROTECTED iv.PUBLIC
  • 4.
    Access Modifiers injava • Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are − • Visible to the package, the default. No modifiers are needed. • Visible to the class only (private). • Visible to the world (public). • Visible to the package and all subclasses (protected).
  • 6.
    1.]DEFAULT ACCESS SPECIFIER: Nokeyword is required to specify default access specifier When no access modifier is specified for a class, method or data member it is said to be having the default access specifier by default. Default access specifier are accessible within the same package.
  • 7.
    2. ]PRIVATE ACCESSSPECIFIER:  The private access specifier is specified using the keyword private.  The methods or data members declared as private are accessible only within the class in which they are declared. Any other class of same package will not be able to access these members.  Classes or interface can not be declared as private.
  • 8.
    PRIVATE Example 1.class A{ 2.privateint data=40; 3.private void msg(){System.out.println("Hello java");} 4.} 5. 6.public class Simple{ 7. public static void main(String args[]){ 8. A obj=new A(); 9. System.out.println(obj.data);//Compile Time Error 10. obj.msg();//Compile Time Error 11. } 12.}
  • 9.
    PRIVATE Example –setter & getter public class Logger { private String format; public String getFormat() { return this.format; } public void setFormat(String format) { this.format = format; } }
  • 10.
    PRIVATE Example –setter & getter class Data { private String name; // getter method public String getName() { return this.name; } // setter method public void setName(String name) { this.name= name; } } public class Main { public static void main(String[] main){ Data d = new Data(); // access the private variable using the getter and setter d.setName(“Ganpat Univ"); System.out.println(d.getName()); } }
  • 11.
    3. ]PROTECTED ACCESS SPECIFIER: Theprotected access specifier is specified using the keyword protected. The methods or data members declared as protected are accessible within same package or sub classes in different package.
  • 12.
    PROTECTED Example class Animal{ // protected method protected void display() { System.out.println("I am an animal"); } } class Dog extends Animal { public static void main(String[] args) { // create an object of Dog class Dog d = new Dog(); // access protected method d.display(); } }
  • 13.
    PROTECTED Example package p1; //ClassA public class A { protected void display() { System.out.println("Learning JAVA"); } } package p2; import p1.*; //importing all classes in package p1 //Class B is subclass of A class B extends A { public static void main(String args[]) { B obj = new B(); obj.display(); } }
  • 14.
    .4]PUBLIC ACCESS SPECIFIER: The public access specifier is specified using the keyword public.  The public access specifier has the widest scope among all other access modifiers.  Classes, methods or data members which are declared as public are accessible from every where in the program. There is no restriction on the scope of a public data members.
  • 15.
    ACCESS LEVELS Specifier ClassPackage Subclass Everywhere Default Y Y N N Private Y N N N Protected Y Y Y N Public Y Y Y Y
  • 16.