0
0
Javaprogramming~15 mins

Default access modifier in Java - Mini Project: Build & Apply

Choose your learning style8 modes available
folder_codeUnderstanding Default Access Modifier in Java
📖 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
💡 Need a 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
💡 Need a 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
💡 Need a 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
💡 Need a hint?

Make sure you run the LibraryTest class, not Library.