-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessModifierEx.java
More file actions
59 lines (41 loc) · 1.76 KB
/
AccessModifierEx.java
File metadata and controls
59 lines (41 loc) · 1.76 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
47
48
49
50
51
52
53
54
55
56
57
58
59
package modify; //modify package
import defaultAcc.Default; // import Default class
import protectedAcc.Protected; // import Protected class
import privateAcc.Private; //import Private class
import publicAcc.Public;//import Public class
/*
* Author :vaibhavi
* code to demonstrate access modifiers in package
*/
public class AccessModifierEx extends Protected{
void callSuperMethods() {
//protected access modifier of protected class is accessible only by inheritance outside the package
super.proMethod(4, 6); //super keyword is used to call Protected class fields
int a=super.proVar;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//create default class object
Default df=new Default();
//int a= df.defaultVar;//error: default not accessible outside the package
Protected pro=new Protected();// protected is accessible outside package
int a=pro.pub;//public variable of Protected class is accessible only
// # public is accessible anywhere
//create object of Public class which have all public fields and methods
Public pb=new Public();
int b=pb.pubVar;
pb.pubMethod(23, 12);
//create object of Private class which have all private fields and methods
Private prv=new Private();
//not accessible outside package
//int pv=prv.privateVar; error
//prv.privMethod; error
/*
* public is accessible anywhere
* private is accessible only inside that class
* default is accessible inside same class and same package
* protected is accessible inside same class , same package and subclass of outside the package
*
*/
}
}