Repository navigation
Expand file tree
/
Copy pathAccessModifiersDemo.java
More file actions
211 lines (174 loc) · 6.61 KB
/
Copy pathAccessModifiersDemo.java
File metadata and controls
211 lines (174 loc) · 6.61 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package src.oop;
/**
* AccessModifiersDemo - Demonstrates Java access modifiers and variable types
*
* This class showcases the four access modifiers in Java:
* - public: accessible from anywhere
* - protected: accessible within package and subclasses
* - default (package): accessible within package only
* - private: accessible within the same class only
*
* @author Java Examples Project
* @version 1.0
*/
public class AccessModifiersDemo {
//-------------------------------------
// Data Type Examples
//-------------------------------------
// Primitive Data Types:
// byte: 8-bit signed integer (-128 to 127)
// short: 16-bit signed integer (-32,768 to 32,767)
// int: 32-bit signed integer (-2^31 to 2^31-1)
// long: 64-bit signed integer (-2^63 to 2^63-1)
// float: 32-bit floating point
// double: 64-bit double-precision floating point
// boolean: true or false
// char: 16-bit Unicode character
//-------------------------------------
// Access Modifier Examples
//-------------------------------------
// PRIVATE: Only accessible within this class
private int privateVariable = 10;
private static int privateStaticCounter = 0;
// PROTECTED: Accessible within package and by subclasses
protected String protectedMessage = "Protected access";
// PUBLIC: Accessible from anywhere the class is accessible
public double publicValue = 3.14159;
// PACKAGE (default): Accessible within the same package only
int packageVariable = 42;
// STATIC: Class-level variable, shared across all instances
public static final String CLASS_NAME = "AccessModifiersDemo";
private static int instanceCount = 0;
// FINAL: Cannot be reassigned after initialization
public final int FINAL_CONSTANT = 100;
private final String finalInstanceVar;
//-------------------------------------
// Constructors
//-------------------------------------
/**
* Default constructor
*/
public AccessModifiersDemo() {
this.finalInstanceVar = "Initialized in constructor";
instanceCount++;
privateStaticCounter++;
}
/**
* Parameterized constructor
* @param value initial value for public variable
*/
public AccessModifiersDemo(double value) {
this();
this.publicValue = value;
}
//-------------------------------------
// Public Methods
//-------------------------------------
/**
* Public method - accessible from anywhere
* @return current private variable value
*/
public int getPrivateVariable() {
return privateVariable;
}
/**
* Public setter with validation
* @param value new value for private variable
*/
public void setPrivateVariable(int value) {
if (value >= 0) {
this.privateVariable = value;
} else {
System.out.println("Value must be non-negative");
}
}
/**
* Demonstrates all access levels within the same class
*/
public void demonstrateAccess() {
System.out.println("=== Access Demonstration ===");
System.out.println("Private variable: " + privateVariable);
System.out.println("Protected message: " + protectedMessage);
System.out.println("Public value: " + publicValue);
System.out.println("Package variable: " + packageVariable);
System.out.println("Final constant: " + FINAL_CONSTANT);
System.out.println("Final instance var: " + finalInstanceVar);
// Can access private methods within same class
privateMethod();
protectedMethod();
}
/**
* Static method example
* @return total number of instances created
*/
public static int getInstanceCount() {
return instanceCount;
}
//-------------------------------------
// Protected Methods
//-------------------------------------
/**
* Protected method - accessible within package and by subclasses
*/
protected void protectedMethod() {
System.out.println("Protected method called");
System.out.println("Private static counter: " + privateStaticCounter);
}
//-------------------------------------
// Package Methods
//-------------------------------------
/**
* Package method - accessible within same package only
*/
void packageMethod() {
System.out.println("Package method called");
}
//-------------------------------------
// Private Methods
//-------------------------------------
/**
* Private method - only accessible within this class
*/
private void privateMethod() {
System.out.println("Private method called");
System.out.println("Only accessible from within AccessModifiersDemo class");
}
/**
* Private helper method for validation
* @param value value to validate
* @return true if valid, false otherwise
*/
private boolean isValidValue(int value) {
return value >= 0 && value <= 1000;
}
//-------------------------------------
// Main method for testing
//-------------------------------------
/**
* Main method to demonstrate access modifiers
* @param args command line arguments
*/
public static void main(String[] args) {
System.out.println("Java Access Modifiers Demonstration");
System.out.println("===================================");
// Create instances
AccessModifiersDemo demo1 = new AccessModifiersDemo();
AccessModifiersDemo demo2 = new AccessModifiersDemo(2.718);
// Demonstrate access within same class
demo1.demonstrateAccess();
System.out.println("\n=== Static Variable Access ===");
System.out.println("Class name: " + CLASS_NAME);
System.out.println("Total instances: " + getInstanceCount());
System.out.println("\n=== Public Access ===");
System.out.println("Demo1 public value: " + demo1.publicValue);
System.out.println("Demo2 public value: " + demo2.publicValue);
// Modify through public methods
demo1.setPrivateVariable(50);
System.out.println("Modified private variable: " + demo1.getPrivateVariable());
// Package access (works within same package)
demo1.packageMethod();
System.out.println("Package variable: " + demo1.packageVariable);
// Protected access (works within same package)
demo1.protectedMethod();
}
}