📖 Scenario: You are learning how Java controls access to classes and their members. In Java, if you do not specify an access modifier, the default access level is package-private. This means the class or member is accessible only within its own package.Imagine you are organizing a small library system where some classes should only be used inside the same package.
🎯 Goal: You will create two classes in the same package. One class will have a method with the default access modifier. You will then call this method from the other class to see how default access works.
📋 What You'll Learn
Create a class named Library with a method void showBooks() that prints "Books available".
Do not specify any access modifier for the showBooks() method (use default access).
Create another class named LibraryTest in the same package.
In LibraryTest, create a main method that creates an object of Library and calls the showBooks() method.
Print the output of calling showBooks().
💡 Why This Matters
🌍 Real World
Understanding access modifiers helps you organize code in large projects, keeping some parts private and others accessible as needed.
💼 Career
Many Java jobs require knowledge of access control to write secure and maintainable code, especially in team environments.
Progress0 / 4 steps
1
Create the Library class with a default access method
Create a class called Library with a method void showBooks() that prints exactly "Books available". Do NOT add any access modifier before void showBooks() so it uses the default access modifier.
Java
Hint
Remember, do not write public, private, or protected before void showBooks(). Just write void showBooks() to use the default access modifier.
2
Create the LibraryTest class with a main method
Create a class called LibraryTest with a public static void main(String[] args) method. Inside main, create an object of Library named library.
Java
Hint
Use Library library = new Library(); inside the main method to create the object.
3
Call the default access method from LibraryTest
In the main method of LibraryTest, call the showBooks() method on the library object.
Java
Hint
Use library.showBooks(); to call the method.
4
Run the program and print the output
Run the LibraryTest class. The program should print exactly "Books available" to the console.
Java
Hint
Make sure you run the LibraryTest class, not Library.
Practice
(1/5)
1. Which statement best describes the default access modifier in Java?
easy
A. It restricts access only to the class itself.
B. It allows access from any package using the default keyword.
C. It allows access only within the same package without using any keyword.
D. It allows access only to subclasses in other packages.
Solution
Step 1: Understand default access modifier meaning
The default access modifier means no keyword is used before a class, method, or variable, and it restricts access to the same package only.
Step 2: Compare options with definition
It allows access only within the same package without using any keyword. correctly states it allows access only within the same package and requires no keyword. Other options mention keywords or access outside the package, which is incorrect.
Final Answer:
It allows access only within the same package without using any keyword. -> Option C
Quick Check:
Default access = package-only access [OK]
Hint: No keyword means default access is package-only [OK]
Common Mistakes:
Thinking default requires a keyword
Confusing default with public or private
Assuming default allows access from other packages
2. Which of the following is the correct way to declare a class with default access modifier?
easy
A. class MyClass {}
B. private class MyClass {}
C. default class MyClass {}
D. public class MyClass {}
Solution
Step 1: Recall syntax for default access modifier
Default access means no access keyword is written before the class name.
Step 2: Check each option
class MyClass {} declares the class without any access keyword, which means default access. Options A and B use explicit keywords, and C uses a non-existent keyword.
Final Answer:
class MyClass {} -> Option A
Quick Check:
No keyword = default access [OK]
Hint: No keyword before class means default access [OK]
Common Mistakes:
Using 'default' keyword which does not exist in Java
Confusing private with default
Always using public by habit
3. Consider two classes in the same package:
class A {
void show() {
System.out.println("Hello from A");
}
}
class B {
public static void main(String[] args) {
A a = new A();
a.show();
}
}
What will be the output when class B is run?
medium
A. Compile-time error: show() has private access
B. Hello from A
C. Compile-time error: class A is not public
D. Runtime error
Solution
Step 1: Analyze access modifiers in the code
Class A and method show() have default access (no keyword). Both classes are in the same package, so access is allowed.
Step 2: Predict program output
Since B can access A and its show() method, the program prints "Hello from A" without errors.
Final Answer:
Hello from A -> Option B
Quick Check:
Default access allows same package access [OK]
Hint: Default access works within same package [OK]
Common Mistakes:
Assuming default means private access
Thinking class A must be public to be used
Confusing compile-time and runtime errors
4. Given two packages pkg1 and pkg2, and these classes:
package pkg1;
class C {
void display() {
System.out.println("Inside C");
}
}
package pkg2;
import pkg1.C;
public class D {
public static void main(String[] args) {
C c = new C();
c.display();
}
}
What is the error in this code?
medium
A. Cannot access class C from pkg2 because it has default access
B. Cannot call display() because it is private
C. Cannot import package pkg1
D. No error, code runs fine
Solution
Step 1: Check class C's access modifier and package
Class C has default access (no keyword) and is in package pkg1, so it is accessible only within pkg1.
Step 2: Check usage of C in pkg2
Class D in pkg2 tries to create an instance of C and call display(), but C is not visible outside pkg1, causing a compile-time error.
Final Answer:
Cannot access class C from pkg2 because it has default access -> Option A
class Helper {
void assist() {
System.out.println("Helping...");
}
}
public class Tool {
public static void main(String[] args) {
Helper h = new Helper();
h.assist();
}
}
You want to allow only classes inside tools to use Helper and its assist() method, but prevent access from outside. Which is the best way to achieve this?
hard
A. Make Helper and assist() public.
B. Use protected for Helper and assist().
C. Make Helper and assist() private.
D. Keep Helper and assist() with default access (no keyword).
Solution
Step 1: Understand access goals
You want Helper and assist() accessible only inside the same package (tools) and hidden outside.
Step 2: Match access modifiers to goals
Default access (no keyword) restricts access to the same package only, which fits the requirement. Public allows all access, private restricts to the class only, and protected allows subclasses and package access.
Final Answer:
Keep Helper and assist() with default access (no keyword). -> Option D
Quick Check:
Default access = package-only visibility [OK]
Hint: Default access hides from outside packages [OK]
Common Mistakes:
Using public which exposes to all packages
Using private which hides even inside package
Using protected which allows subclass access outside package
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.