Access modifiers
3. package/default
3. package/default
Package or default modifier does not have a keyword. A package class or member is accessible only from classes and members in the same package.
4. private
A private class or member is accessible only from the class where it is defined.
The following table shows the access level of the above access modifiers.
Keyword Class Package Subclass Outside package
public Yes Yes Yes Yes
package/default Yes Yes No No
protected Yes Yes Yes No
private Yes No No No
To use a public class from other packages in your own classes, you must import it.
In chapter 7, we have used the classes Strings and StringBuffers in our classes without importing them. The reason is that Strings and StringBuffers are in the package java.lang which is automatically imported in all your classes. If you have a class defined in a different Java package, you cannot use them without importing them.
If you, for example, want to use the class ArrayList in the package java.util, you need to use the following import statement in your class: import java.util.ArrayList; If you need to use many classes in the package java.util, you don't need to import each class separately instead you can use .* at the end of the package. This statement means that you import the whole package:
import java.util.*;
Example
import java.util.ArrayList;
public class MijnKlasse {
public static void main() {
ArrayList<String> automerken = new ArrayList<String>();
automerken.add("Volvo");
automerken.add("BMW");
automerken.add("Toyota");
} }
So far we have used only one class in each quiz. From now on you can expect one or more classes in the quizzes.
Quiz 1: Access class members from the same package
What happens when the following program is compiled and run?
public class ClassB {
public int w = 1;
protected int x = 2;
int y = 3;
private int z = 4;
}
public class ClassA {
public static void main(String[] args) {
ClassB cb = new ClassB();
// System.out.print(cb.w); /* 1 */
// System.out.print(cb.x); /* 2 */
// System.out.print(cb.y); /* 3 */
// System.out.print(cb.z); /* 4 */
} }
Which statement(s) are true? Choose all that apply.
a. If statement 1 is uncommented, the code writes "1" to the standard output.
b. If statement 2 is uncommented, the code writes "2" to the standard output.
c. If statement 3 is uncommented, the code writes "3" to the standard output.
d. If statement 4 is uncommented, the code writes "4" to the standard output.
Explanation
The answers a, b and c are true because both classes are in the same package.
Within the same package, access is allowed to the public, protected and default modifiers.
Answer "d" is incorrect because private members cannot be accessed from other classes.
The correct answers are a, b, c.
Assignments
1. Declare a character type variable myChar in the class ClassB, the variable myChar shouldn't be accessible through the object cb.
2. Add a statement to the classA to test whether myChar is accessible.
Quiz 2: Different packages and access modifiers
What happens when the following program is compiled and run?
package package_02;
public class ClassB {
public int w = 1;
protected int x = 2;
int y = 3;
private int z = 4;
}
package package_01;
import package_02.ClassB;
public class ClassA {
public static void main(String[] args) {
ClassB cb = new ClassB();
// System.out.print(cb.w); /* 1 */
// System.out.print(cb.x); /* 2 */
// System.out.print(cb.y); /* 3 */
// System.out.print(cb.z); /* 4 */
} }
Which statement(s) are true? Choose all that apply.
a. If statement 1 is uncommented, the code writes "1" to the standard output.
b. If statement 2 is uncommented, the code writes "2" to the standard output.
c. If statement 3 is uncommented, the code writes "3" to the standard output.
d. If statement 4 is uncommented, the code writes "4" to the standard output.
Explanation
a. is the only correct answer because the variable "w" is defined as public.
The variable "x" is protected, and it is in the class ClassB, which is outside the package.
That applies to the variables "y" and "z" as well, because their access modifiers are package and private.
The correct answer is a.
Assignments
1. Declare an integer variable called myInt in the ClassB; the object cb should have access to your variable.
2. Write a statement in ClassA to check out the accessibility of your variable.
3. ClassA and ClassB in this example are in two different packages, while in the previous one, they were in the same package.
Analyze the difference between this code and the previous one (qz01).
Quiz 3: Different packages and access modifiers
What happens when the following program is compiled and run?
package package_02;
public class ClassB {
public int w = 1;
protected int x = 2;
int y = 3;
private int z = 4;
public void myMethod() {
System.out.print(w);
System.out.print(x);
System.out.print(y);
System.out.print(z);
} }
package package_01;
import package_02.ClassB;
public class ClassA {
public static void main(String[] args) {
ClassB cb = new ClassB();
cb.myMethod();
} }
Which statement(s) are true? Choose all that apply.
a. This code writes "1234" to the standard output.
b. If you remove the "public" modifier from the ClassB, the code writes "1234" to the standard output.
c. If you remove the "public" modifier from the method myMethod, the code writes "1234" to the standard output.
d. This code does not compile.
Explanation
a. ClassA can access all the public methods of the ClassB.
ClassA can access the private, protected and default members of classB indirectly through the public method myMethod.
b. If you remove the public modifier from the ClassB, you cannot access it anymore from ClassA. Because the two classes are in two different packages.
c. If you remove the public modifier from the method myMethod, you can not access the method from the ClassA.
Answer d is not correct.
The correct answer is a.
Assignment
What do you expect if you remove the public keyword from the classB?
Assignment chapter 8: The classes Date and Student in different packages
1. Create two packages, calendar, and personal_data
2. Create a class called DateInfo inside the package calendar
3. The class DateInfo has only three variables, day (type integer), month (integer), year (integer).
4. The three variables day, month and year, are invisible outside the class DateInfo.
5. Define a constructor for the class DateInfo with three-arguments to initialize the variables day, month and year.
6. Write a method called getDateFormat, that returns a date format example;
19/4/2016 (the method returns a String type for simplification) 7. Create a class called Student inside the package personal_data.
8. The Student class has only two variables, name (type String) and birthDate (type DateInfo).
9. Create two student objects for Isabella and David, Isabella's birthDate is 28/8/1998 and David's birthDate is 13/9/1996
10. Write an extra piece of code to write the names and the birth dates of the two students to the standard output as shown below.
---First Student---Name: Isabella
Birth date: 28/8/1998 ---Second Student---Name: David
Birth date: 13/9/1996