Access Modifiers
1. private
2. protected
3. default
4. public
public access modifier:
Fields, methods and constructors declared public within a public class are visible to any class in the Java program, whether these classes are in the same package or in another package.
Ex: public String name;
private access modifier:
they cannot be accesses by anywhere outside the enclosing class. A standard design strategy is to make all fields private and provide public getter and setter methods for them.
Ex: private int balance;
protected access modifier:
Fields, methods and constructors declared protected in a class(say superClass) is accessed in same class,same package and subclass deriving the class(superClass)
Ex: protected float age;
default access modifier:
Any class, field, method or constructor that has no declared access modifier is accessible only by classes in the same package.
Ex: String interest
1. private
2. protected
3. default
4. public
public access modifier:
Fields, methods and constructors declared public within a public class are visible to any class in the Java program, whether these classes are in the same package or in another package.
Ex: public String name;
private access modifier:
they cannot be accesses by anywhere outside the enclosing class. A standard design strategy is to make all fields private and provide public getter and setter methods for them.
Ex: private int balance;
protected access modifier:
Fields, methods and constructors declared protected in a class(say superClass) is accessed in same class,same package and subclass deriving the class(superClass)
Ex: protected float age;
default access modifier:
Any class, field, method or constructor that has no declared access modifier is accessible only by classes in the same package.
Ex: String interest
|
|
Same Class
|
Same Package
|
Subclass
|
Other packages
|
|
public
|
Y
|
Y
|
Y
|
Y
|
|
protected
|
Y
|
Y
|
Y
|
N
|
|
default
|
Y
|
Y
|
N
|
N
|
|
private
|
Y
|
N
|
N
|
N
|
No comments:
Post a Comment