Declarations and Access Control
Declarations and Access Control
Access Modifiers
Access modifiers are an important part of any declaration that can be accessed outside the class or package in which it is made. Access modifiers enable you to determine whether a declaration is limited to a particular class, a class and its subclasses, a package, or if it is freely accessible. Although access modifiers are not iron-clad security controls, they can be used to limit access to your classes, interfaces, variables, methods, and constructors. Java provides three access modifiers (public, protected, and private) and a fourth default access (package access). These modifiers are used as follows: publicEnables a class or interface to be accessed outside of its package. It also enables a variable, method, or constructor to be accessed anywhere its class may be accessed. protectedEnables a variable, method, or constructor to be accessed by classes or interfaces of the same package or by subclasses of the class in which it is declared. privatePrevents a variable, method, or constructor from being accessed outside of the class in which it is declared. Package accessPackage access occurs when public, protected, or private are not specified. Package access applies to classes, interfaces, variables, methods, and constructors. It allows the declared item to be accessed by any class or interface of the same package. Table 4.1 identifies the language elements that may be declared using the above access modifiers and the restrictions that are placed on these language elements as the result of these modifiers.
Exercises:
1. What is the output of the following program? class Question extends SuperClass { String s = "this"; public static void main(String[] args) { new Question(); } Question() { super.display(s); } void display(String s) { System.out.println("this: "+s); } } class SuperClass { String s = "super"; void display(String s) { System.out.println("super: "+s); } } A. this: this B. super: this C. this: super D. super: super 2. Which of the following declare an array of String objects? A. String[] s; B. String []s; C. String [s]; D. String s[]; 3. What is the value of a[3] as the result of the following array declaration? int[] a = {1,2,3,4,5}; A. 1 B. 2 C. 3 D. 4 4. Which of the following are true about this method declaration? void myMethod(String s) { } A. myMethod() is static. B. myMethod() does not return a value. C. myMethod() is abstract. D. myMethod() may not be accessed outside of the package in which it is declared. 5. Which of the following are true about this variable declaration? private static int i = 3; A. The value of i may not be changed after it is assigned a value. B. i may only be updated by a static method. C. The value of i is shared among all instances of the class in which it is
declared. D. i may only be accessed within the class in which it is declared. 6. What is wrong with the following program? class Question { String s = "abc"; public static void main(String[] args) { System.out.println(s); } } A. Nothing is wrong with the program. B. main() cannot be declared public because Question is not public. C. Because main() is static, it may not access non-static s without a reference to an instance of Question. D. The main() argument list is incorrect. 7. Which of the following are true? A. A top-level class may be declared as private. B. A method may be declared as transient. C. A constructor may be declared as volatile. D. A local variable may be declared as final. 8. What is the output of the following program? class Question { static int i = 1; static { ++i; } public static void main(String[] args) { increment(i,5); display(i); } static void increment(int n, int m) { n += m; } static void display(int n) { System.out.print(n); } static { ++i; } } A. 1 B. 3 C. 6 D. 7 9. Which of the following are true? A. Local variables may be declared final. B. Methods may be declared final. C. Constructors may not be declared private. D. transient variables may not be serialized.
10. What is the output of the following program? class Question { static int i = 1, j = 2; static { display(i); } public static void main(String[] args) { display(j); } static void display(int n) { System.out.print(n); } } A. 1 B. 2 C. 12 D. 21
More
1. Which is true? (Choose all that apply.) A. "X extends Y" is correct if and only if X is a class and Y is an interface B. "X extends Y" is correct if and only if X is an interface and Y is a class C. "X extends Y" is correct if X and Y are either both classes or both interfaces D. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces 2. Which method names follow the JavaBeans standard? (Choose all that apply.) A. addSize B. getCust C. deleteRep D. isColorado E. putDimensions 3. Given:
1. 2. 3. 4. 5. 6. 7. class Voop { public static void main(String [] args) { doStuff(1); doStuff(1,2); } // insert code here }
Which, inserted independently at line 6, will compile? (Choose all that apply.) A. static void doStuff(int... doArgs) { } B. static void doStuff(int[] doArgs) { } C. static void doStuff(int doArgs...) { } D. static void doStuff(int... doArgs, int y) { } E. static void doStuff(int x, int... doArgs) { } 4. Given:
1. 2. 3. 4. 5. enum Animals { DOG("woof"), CAT("meow"), FISH("burble"); String sound; Animals(String s) { sound = s; } }
6. class TestEnum { 7. static Animals a; 8. public static void main(String[] args) { 9. System.out.println(a.DOG.sound + " " + a.FISH.sound); 10. } 11. }
What is the result? A. woof burble B. Multiple compilation errors C. Compilation fails due to an error on line 2 D. Compilation fails due to an error on line 3 E. Compilation fails due to an error on line 4 F. Compilation fails due to an error on line 9 5. Given two files:
1. package pkgA; 2. public class Foo { 3. int a = 5; 4. protected int b = 6; 5. public int c = 7; 6. } 3. package pkgB; 4. import pkgA.*; 5. public class Baz { 6. public static void main(String[] args) { 7. Foo f = new Foo(); 8. System.out.print(" " + f.a); //will spawn error 9. System.out.print(" " + f.b); //will spawn error, needs inheritance 10. System.out.println(" " + f.c); 11. } 12. }
What is the result? (Choose all that apply.) A. 5 6 7 B. 5 followed by an exception C. Compilation fails with an error on line 7 D. Compilation fails with an error on line 8 E. Compilation fails with an error on line 9 F. Compilation fails with an error on line 10 6. Given:
1. public class Electronic implements Device { public void doIt() { } } 2. 3. abstract class Phone1 extends Electronic { } 4. 5. abstract class Phone2 extends Electronic { public void doIt(int x) { } } 6. 7. class Phone3 extends Electronic implements Device { public void doStuff() { } } 8. 9. interface Device { public void doIt(); }
What is the result? (Choose all that apply.) A. Compilation succeeds B. Compilation fails with an error on line 1 C. Compilation fails with an error on line 3 D. Compilation fails with an error on line 5 E. Compilation fails with an error on line 7 F. Compilation fails with an error on line 9 7. Given:
4. class Announce { 5. public static void main(String[] args) { 6. for(int __x = 0; __x < 3; __x++) ;
7. int #lb = 7; 8. long [] x [5]; 9. Boolean []ba[]; 10. enum Traffic { RED, YELLOW, GREEN }; 11. } 12. }
What is the result? (Choose all that apply.) A. Compilation succeeds B. Compilation fails with an error on line 6 C. Compilation fails with an error on line 7 D. Compilation fails with an error on line 8 E. Compilation fails with an error on line 9 F. Compilation fails with an error on line 10 8. Given:
3. public class TestDays { 4. public enum Days { MON, TUE, WED }; 5. public static void main(String[] args) { 6. for(Days d : Days.values() ) 7. ; 8. Days [] d2 = Days.values(); 9. System.out.println(d2[2]); 10. } 11. }
What is the result? (Choose all that apply.) A. TUE B. WED C. The output is unpredictable D. Compilation fails due to an error on line 4 E. Compilation fails due to an error on line 6 F. Compilation fails due to an error on line 8 G. Compilation fails due to an error on line 9 9. Given:
4. public class Frodo extends Hobbit { 5. public static void main(String[] args) { 6. Short myGold = 7; 7. System.out.println(countGold(myGold, 6)); 8. } 9. } 10. class Hobbit { 11. int countGold(int x, int y) { return x + y; } 12. }
What is the result? A. 13 B. Compilation fails due to multiple errors //depends on staticity of context C. Compilation fails due to an error on line 6 D. Compilation fails due to an error on line 7 E. Compilation fails due to an error on line 11