-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessModifiers.java
More file actions
46 lines (40 loc) · 1.68 KB
/
AccessModifiers.java
File metadata and controls
46 lines (40 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import mycustompackage.*; // Import the package of UDF Package
//! Access Modifier Accessibility
//? within within outside package outside
//? class package by subclass package
//* public Y Y Y Y
//* protected Y Y Y N
//* Default Y Y N N
//* private Y N N N
class Ex1 {
public int num1 = 10; // (Public Access Modifiers)
private int num2 = 20; // (Private Access Modifiers)
protected int num3 = 30; // (Protected Access Modifiers)
int num4 = 40; // (Default Access Modifiers)
public void sum() {
// We can access all the property of the class
System.out.println("Access the property using method!");
System.out.println(num1);
System.out.println(num2);
System.out.println(num3);
System.out.println(num4);
}
// ! Withing subclass we can only access public and private access modifiers but
// cannot protected and default
}
public class AccessModifiers {
public static void main(String[] args) {
// ? Create instance of th Ex1 Class
Ex1 ex = new Ex1();
ex.sum();
// ? We access the property of the class
System.out.println();
System.out.println("Access the property within package!");
System.out.println(ex.num1);
// Here you can see that we can not access private access modifiers in the same
// package
// System.out.println(ex.num2);
System.out.println(ex.num3);
System.out.println(ex.num4);
}
}