•Encapsulation provides another important attribute called “access control”.
•Access Control is used to control the access to the class members.
•In which parts of the program a class member is accessible, is determined by the “access specifier”/”access modifier”.
Types of Access Specifiers:
In Java there are four types of access specifiers. They are:
1) public
2) protected
3) default / no-modifier / package-private
4) private
•public class members are accessible within the class, outside the class and from everywhere else.
•protected class members are accessible within the class, in the subclasses within the same package and in subclasses in other packages.
•default class members are accessible within the class, in the subclasses within the same package only.
•private class members are accessible within the class only.
Using Access Specifiers:
•Syntax for declaring a variable using access specifier:
<access-specifier> type variable-name;
Example: public int a;
•Syntax for declaring a method using access specifier:
<access-specifier> return-type method-name()
{ statement(s); }
Example:
public void display()
{
System.out.println(“hello world”);
}
•Access Control is used to control the access to the class members.
•In which parts of the program a class member is accessible, is determined by the “access specifier”/”access modifier”.
Types of Access Specifiers:
In Java there are four types of access specifiers. They are:
1) public
2) protected
3) default / no-modifier / package-private
4) private
•public class members are accessible within the class, outside the class and from everywhere else.
•protected class members are accessible within the class, in the subclasses within the same package and in subclasses in other packages.
•default class members are accessible within the class, in the subclasses within the same package only.
•private class members are accessible within the class only.
Using Access Specifiers:
•Syntax for declaring a variable using access specifier:
<access-specifier> type variable-name;
Example: public int a;
•Syntax for declaring a method using access specifier:
<access-specifier> return-type method-name()
{ statement(s); }
Example:
public void display()
{
System.out.println(“hello world”);
}
No comments:
Post a Comment