2. Access Modifiers
Helps to restrict the scope of a class, constructor , variable , method or data
member.
Types
• Public
• Private
• Protected
• Default
3. Real time example
Bed Room
Hall
Reception
Door Step
Producted and default
Public
Private
6. Public
• The public access modifier is specified using the keyword public.
• The members, methods and classes that are declared public can be
accessed from anywhere.
7. //Predict the output
package one;
public class A
{
public void display()
{
System.out.println("Java");
}
}
import one.*;
class B
{
public static void main(String args[])
{
A obj = new A;
obj.display();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
8. //Creating Packing countdigits
package countdigits;
public class First
{
public void no_of_digits()
{
int count = 1;
while(num>0){
num=num/10;
count++;
}
System.out.print(count);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
9. //Predict the output
import countdigits.*;
class Second
{
public static void main(String args[])
{
First obj = new First();
obj.no_of_digits(456);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
10. //Predict the output
class Log{
public int debugLevel = 1;
public void debug(String logLine){
System.out.println("Debug: "+logLine);
}
public void info(String logLine){
System.out.println("Information: "+logLine);
}
}
public class Main{
public static void main( String[] args ) {
Log log = new Log();
log.debug("debug with level " + log.debugLevel);
log.debugLevel = 5;
log.info("information with level " + log.debugLevel);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
12. Private
• The private access modifier 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.
13. //Predict the output
class A{
private double num = 100;
private int add(int a,int b){
return a+a;
}
}
public class Example{
public static void main(String args[]){
A obj = new A();
System.out.println(obj.num);
System.out.println(obj.add(10,20));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
14. //Predict the output
public class PersonalData {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.format = name;
}
}
public class UnknownPerson {
Public static void main(String[] main){
PersonalData d = new PersonalData();
d.setName(“Mansoor”);
System.out.println(d.getName());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
15. //Predict the output
class Data
{
private String name;
public String getName(){
return this.name;
}
public void setName(String name)
{
this.name = name;
}
}
public class Main
{
public static void main(String[] main)
{
Data d = new Data();
d.setName("Focus");
System.out.println(d.getName());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
17. Protected
• The protected access modifier is specified using the keyword protected.
• It can be accessed within the same package classes and sub-class of any
other packages.
18. //Predict the output
package package1;
public class Product {
protected int multiplication(int a, int b){
return a*b;
}
}
package package2;
import package1.*;
class Test extends Product{
public static void main(String args[]){
Test obj = new Test();
System.out.println(obj.multiplication(11,10));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
19. //Predict the output
class Logger
{
protected void debug(String logLine)
{
System.out.println("Debug line: "+logLine);
}
}
public class Main extends Logger
{
public static void main(String [] args)
{
Main logger = new Main();
logger.debug("Main method executed");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
20. Default
• The data members, class or methods which are not declared using any
access modifiers.
• It is accessible only within the same package.
21. //Predict the output
class Logger
{
void message()
{
System.out.println("This is a default");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
22. //Predict the output
package package1;
public class Product {
int multiplication(int a, int b){
return a*b;
}
}
package package2;
import package1.*;
class Test {
public static void main(String args[]){
Product obj = new Product();
System.out.println(obj.multiplication(11,10));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23. Access Modifiers Visibility
Public Visible to All classes.
Private
Visible with in the class. It is not accessible outside the
class.
Protected
Visible to classes with in the package and the subclasses
of other package.
No Access Modifier (Default) Visible to the classes with the package
Description:
Home is an real time example for access modifiers.
For example, let us consider a house, The bedrooms of the house are given private access. that is, it is accessible to only the members of the family. The protected access is accessible within package and outside the package but through inheritance only.
Description:
In facebook If you given public in your profile then anyone can see your details, status. In general public is used to give access within or outside
Output:
Java
Description:
Package one contains the class A. It contains a method called display() which is declared as public.
Then we are importing all the classes of the package one. Since the method in package one is declared as public. It can be accessed outside the class
Description:
Creating a user defined Package called countdigits. Package countdigits contains the class First. It contains a method no_of_digits() which is declared as public. It is used find the number of digits in the given digit.
Output:
3
Output:
Debug: debug with level 1
Information: information with level 5
Description:
Here, in Main class, you were able to instantiate the Log class because it’s access modifier is public. The variables and methods inside the Main class are also public. Hence, you are able to use it directly in your Main class.
Description:
In facebook If you given Only me in your profile then only you can see your details, status. In general private is used to give access within the class. i.e.)It can be modified only by the user
Note: Class or Interface cannot be declared as private.
Output:
Error
Description:
throws compilation error because we are trying to access the private data member and method of class A.
Output:
Error
Output:
Focus
Description:
Here, main class in data class is directly access the “class Data” , so it will print the output of “Focus”.
Description:
Note: Class or Interface cannot be declared as private.
Output:
110
Description:
The class Test which is present in another package is able to call the multiplictaion() method, which is declared protected.
This is because the Test class extends class Product and the protected modifier allows the access of protected members in subclasses (in any packages).
Output:
Debug line: Main method executed
Note: Class or Interface cannot be declared as private.
Output:
Compilation error
Description:
Error: Main method not found in class Logger, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Output:
Compilation error
Description:
Because we didn’t mention any access modifiers for the method multiplication in class product which is in package called package1. So it becomes default access specifier. It can be accessed with in the same package.
In this program we are trying to access outside the package package1.