Java Access Modifier
basically Java access Modifiers are four type as below :
1. Private
2.Default
3.Public
4. Protected
non-Access modifiers are : Static , Abstract , native and Synchronized etc.
1. Private Access Modifier : private access modifier is accessible only within class.
2. Default : it is accessible only within package.
3. Public : public access modifier is access in every where .
4. Protected : its accessible within package or outside package but throw inheritance only.
below are the table for access modifier:
Example for all:
package indrajeet.com.defaultpackage.test;
public class DefaultTestClass {
int a=40;
}
package indrajeet.com.defaultpackage.test;
public class ProtectedTestClass {
protected int b=20;
protected void getAccess()
{
System.out.println(b);
}
}
package indrajeet.com.defaultpackage.test.importpackage;
import indrajeet.com.defaultpackage.test.DefaultTestClass;
public class AcessModifierMainclass {
public static void main(String[] args) {
DefaultTestClass object1=new DefaultTestClass();
//System.out.println(object1.a); default access modifier can be access only within package.
ProtectedAccessClass object2=new ProtectedAccessClass(5);
ProtectedAccessClass object3=new ProtectedAccessClass();
}
}
class ProtectedAccessClass extends ProtectedTestClass {
public ProtectedAccessClass()
{
this(3);
}
public ProtectedAccessClass(int i) {
System.out.println(i);
}
}
basically Java access Modifiers are four type as below :
1. Private
2.Default
3.Public
4. Protected
non-Access modifiers are : Static , Abstract , native and Synchronized etc.
1. Private Access Modifier : private access modifier is accessible only within class.
2. Default : it is accessible only within package.
3. Public : public access modifier is access in every where .
4. Protected : its accessible within package or outside package but throw inheritance only.
below are the table for access modifier:
Access Modifier | within class | within package | outside package by subclass only | outside package |
private | Yes | No | No | No |
Default | Yes | yes | No | No |
Protected | yes | yes | Yes | No |
public | yes | yes | yes | Yes |
Example for all:
package indrajeet.com.defaultpackage.test;
public class DefaultTestClass {
int a=40;
}
package indrajeet.com.defaultpackage.test;
public class ProtectedTestClass {
protected int b=20;
protected void getAccess()
{
System.out.println(b);
}
}
package indrajeet.com.defaultpackage.test.importpackage;
import indrajeet.com.defaultpackage.test.DefaultTestClass;
public class AcessModifierMainclass {
public static void main(String[] args) {
DefaultTestClass object1=new DefaultTestClass();
//System.out.println(object1.a); default access modifier can be access only within package.
ProtectedAccessClass object2=new ProtectedAccessClass(5);
ProtectedAccessClass object3=new ProtectedAccessClass();
}
}
class ProtectedAccessClass extends ProtectedTestClass {
public ProtectedAccessClass()
{
this(3);
}
public ProtectedAccessClass(int i) {
System.out.println(i);
}
}