Theory
The helper that should stay backstage
Inside the circulation package you write FineTable, a small helper that maps days-late to rupees. It is plumbing: if the reports package (your teammate's code) starts calling it directly, every change you make breaks his month-end report.
You want FineTable usable by every circulation class and invisible to everyone else.
Remember the mysterious no-modifier access level from Unit 2, defined as "the whole package"? This is the lesson where it earns its living.
Theory
What lives inside a package
A package's contents are:
- classes (Book, IssueRegister, FineTable)
- interfaces (Printable, Payable)
- exception classes (OverdueException: it is a class too)
- subpackages (circulation.fines), which are separate packages that merely share the name prefix
One physical rule with exam weight: a public class must be declared in a file named exactly after it (public class IssueRegister only in IssueRegister.java), and a file may hold at most one public class.
At a glance
Access levels, seen from another package
| Member marked | Same package sees it? | Other package sees it? |
|---|---|---|
| private | No (own class only) | No |
| (default, no modifier) | Yes | No |
| protected | Yes | Only its subclasses |
| public | Yes | Yes |
Practical
Backstage by default
// file: circulation/FineTable.java
package circulation;
class FineTable { // no 'public': package-private CLASS
static int fineFor(int daysLate) {
return daysLate * 2; // Rs 2 per late day
}
}
// file: circulation/ReturnDesk.java
package circulation;
public class ReturnDesk { // the package's public face
public int settle(int daysLate) {
return FineTable.fineFor(daysLate); // same package: fine
}
}
// In package reports, both lines below would be COMPILE ERRORS:
// circulation.FineTable.fineFor(3); class not visible
// new circulation.FineTable(); class not visible
Quiz
class Helper { } (no modifier) sits in package circulation. Code in package reports tries new circulation.Helper(); What happens?
- Works: the fully qualified name bypasses access rules
- Compile error: a default-access class is invisible outside its package
- Works, but only Helper's public members are usable
- Runtime exception when the constructor executes
Show the answer
Compile error: a default-access class is invisible outside its package
No modifier on the CLASS means package-private: to every other package, Helper simply does not exist, and the compiler says so. Option A is the tempting myth this question kills: a fully qualified name is an address, not a key; it never overrides access control. Option C would require the class itself to be visible first. Option D is too late: like every access violation in Java, this dies at compile time.
Think first
Two Reports, no fight
Your circulation package and your teammate's reports package BOTH declare a class named Summary. Before tapping: does the project still compile, and how does Java tell them apart?
Show the answer
It compiles happily. The real name of a class is its fully qualified name: circulation.Summary and reports.Summary are entirely different names that merely end alike, the way 2 students named Riya are distinguished by surname. This is packages acting as namespaces, the second big reason they exist besides access control. A file needing both at once imports neither and writes the full names out.
Watch out
Where package access surprises people
Forgetting public on the class: everything inside can be public, but if the CLASS is default, no other package can touch any of it. Check the class's own modifier first when an import mysteriously fails.
protected across packages: a protected member reaches another package ONLY inside a subclass. Unrelated classes there see nothing; and Java's protected still includes the whole home package, wider than BCA304's C++ rule.
Theory
Design with the boundary
Professional Java uses the package line deliberately: a few public classes as the shelf's front (ReturnDesk), helpers left package-private behind them (FineTable). Change the plumbing freely; outsiders never depended on it. That is encapsulation from Unit 2 scaled up from one class to a whole shelf. The last package lesson asks how the machinery FINDS all these shelves at run time: folders, classpath, and the Package object.
Summary
Key takeaways
- A package contains classes, interfaces, exception classes and subpackages.
- Default (no-modifier) access = package-private: visible everywhere inside the package, invisible outside it.
- A default-access CLASS is unusable from other packages, whatever its members say.
- public class Name must live in Name.java; one public class per file.
- protected = whole home package + subclasses in other packages.
- Same class name in 2 packages is fine: fully qualified names are the real names.
- Memory hook: the package is the shelf; no-modifier means staff only.