0% found this document useful (0 votes)
102 views6 pages

Package Public Class Int Private Int Public Int Protected Static Int Public Static Void

Access specifiers in Java control the visibility and scope of classes, methods, and fields. There are 4 access specifiers: private, default, protected, and public. Private members are only accessible within the class, default within the package, protected within the package and subclasses, and public anywhere. The document provides examples demonstrating how each access specifier controls accessibility.

Uploaded by

Awadhesh HK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views6 pages

Package Public Class Int Private Int Public Int Protected Static Int Public Static Void

Access specifiers in Java control the visibility and scope of classes, methods, and fields. There are 4 access specifiers: private, default, protected, and public. Private members are only accessible within the class, default within the package, protected within the package and subclasses, and public anywhere. The document provides examples demonstrating how each access specifier controls accessibility.

Uploaded by

Awadhesh HK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Access specifiers:

Access specifiers are use to represent scope of members of class. In java Access specifiers
are classified into 4 types/ access modifier/ access provider/visibility labels

1. private

2. default

3. protected

4. public

1. private: If you declare any member of class as private then scope of that member
remains only within the class. It can't be access from other classes.

2. default: If you declare any member of class as default then scope of that member
remains only within the package. It can't be access from other packages.
Note: There is no keyword to represent default access specifier. By default java classes
access specifires will be default until we make it public, private, protected

3. protected: If you declare any member of class as protected then scope of that member
remains only within the package, that class which is present outside the package can
access it by one condition ie. inheritance operation.

4. public: If you declare any member of class as public then scope of that member remains
throught the project.

package Access_modifier;

public class Private_use {


int a=20;
private int b=50;
public int c=60;
protected static int d=80;
public static void main(String[] args) {
Private_use u= new Private_use();
u.add();//calling private method--
allowed within class
u.sub();// calling public method--
allowed within project
u.mul();// calling default method--
allowed within package
display();//calling protected method--
allowed in other packed(use inheritance)
System.out.println("value of a is
"+u.a);
System.out.println("value of b is
"+u.b);
System.out.println("value of c is
"+u.c);
System.out.println("value of d is
"+u.d);
}

private void add() { // access


specifier private
int a,b=10,c=20;
a=b+c;
System.out.println("value of a is "+a);
}
public void sub() { // access specifier
public
int a,b=20,c=10;
a=b-c;
System.out.println("value of a is "+a);
}
void mul() { // access specifier
default
int a,b=20,c=10;
a=b*c;
System.out.println("value of a is "+a);
}
protected static void display() // access
specifier protected
{
System.out.println("hi i am protected
method");
}
}

package Access_modifier;

public class Access_test { // created class in


same package

public static void main(String[] args) {


Private_use p= new Private_use();//
creating object of other class
p.sub();// calling public method of
other class within same package-- allowed in
project
//p.add();// calling private method of
other class within same package--not allowed in
other class
p.mul();//calling default method of
other class within same package-- allowed
within package
Private_use.display();//calling
protected method of other class within same
package-- allowed in same package/ need
inheritance to call in another package
System.out.println("value of a is
"+p.a);
//System.out.println("value of a is
"+p.b);
System.out.println("value of c is
"+p.c);
System.out.println("value of d is
"+p.d);

}
package constructor_study;

import Access_modifier.Private_use;
public class Accses_use extends Private_use
{// created class in another package

public static void main(String[] args) {


Private_use ps= new Private_use();//
created object of super class
ps.sub();// calling public method of
another class in other package-- allowed in
project
//ps.add();// calling private method of
other class in other package--not allowed in
other class
//ps.mul();//calling default method of
other class in other package-- allowed within
package
Private_use.display();//calling
protected method of other class within same
package-- allowed in same package/ need
inheritance to call in another package
//System.out.println("vlaue of a is
"+ps.a);
//System.out.println("vlaue of b is
"+ps.b);
System.out.println("vlaue of c is
"+ps.c);
System.out.println("vlaue of d is
"+Private_use.d);

}
}

You might also like