Packages & Access Modifiers
785 words
4 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Packages & Access Modifiers ## 🎯 Learning Objectives - Declare and use packages to organize code - Understand the four access modifiers - Use `import` statements effectively - Follow Java package naming conventions ## 1. Packages — Namespaces for Classes ### 1.1 What Problem Do They Solve?

Packages & Access Modifiers
🎯 Learning Objectives
- Declare and use packages to organize code
- Understand the four access modifiers
- Use
importstatements effectively - Follow Java package naming conventions
1. Packages — Namespaces for Classes
1.1 What Problem Do They Solve?
Without packages, you can't have two classes named
Date in the same project. Packages provide:- Namespace management:
java.util.Datevsjava.sql.Date - Access control: package-private access
- Organization: logical grouping of related classes
1.2 Package Declaration
java// File: com/iitm/student/Student.java package com.iitm.student; public class Student { // ... }
1.3 Directory Structure
pseudosrc/ com/ iitm/ student/ Student.java course/ Course.java
Package name = directory path. Convention:
com.yourorganization.project.module1.4 Import Statements
java// Single class import import java.util.ArrayList; // Wildcard import import java.util.*; // Static import (for constants/static methods) import static java.lang.Math.PI; import static java.lang.Math.sqrt; public class Test { double area = PI * r * r; // No Math.PI needed double dist = sqrt(x); // No Math.sqrt needed }
Import notes:
java.lang.*is automatically imported- Wildcard imports don't hurt performance (compiler resolves at compile time)
- Explicit imports are preferred for clarity
2. Access Modifiers — Controlling Visibility
2.1 The Four Modifiers
| Modifier | Class | Package | Subclass | World |
|---|---|---|---|---|
private | ✓ | ✗ | ✗ | ✗ |
| default (none) | ✓ | ✓ | ✗ | ✗ |
protected | ✓ | ✓ | ✓ | ✗ |
public | ✓ | ✓ | ✓ | ✓ |
2.2 Examples
javapackage com.iitm; public class VisibilityDemo { private int privateVar = 1; // Only within this class int packageVar = 2; // Within package protected int protectedVar = 3; // Package + subclasses public int publicVar = 4; // Everyone private void privateMethod() { } void packageMethod() { } protected void protectedMethod() { } public void publicMethod() { } }
(Diagram)
2.3 Best Practices
- Fields: Prefer
private, expose via getters/setters - Constants:
public static final - Internal methods:
privateor package-private - API methods:
public - Inheritance hooks:
protected
3. Common Pitfalls
Pitfall 1: Package Name Doesn't Match Directory
If
package com.iitm.student; but file is in src/student/Student.java — compilation fails.Pitfall 2: Protected Access in Different Package
java// Package A: public class Parent { protected void method() { } } // Package B: public class Child extends Parent { public void test() { method(); // OK (inherited) Parent p = new Parent(); p.method(); // ERROR! Can only access via inheritance reference } }
Pitfall 3: Importing Both java.util.Date and java.sql.Date
javaimport java.util.*; import java.sql.*; // Date is ambiguous — must use fully qualified names java.util.Date d1 = new java.util.Date(); java.sql.Date d2 = new java.sql.Date(0);
4. Practice Questions
Q1: What's the difference between default (package-private) and protected?Answer: Both are accessible within the same package.protectedadditionally allows access in subclasses (even in different packages). Default does not. Q2: Can a private method be overridden?Answer: No. Private methods are not visible to subclasses, so they cannot be overridden. A subclass can declare a method with the same name — it's a new method, not an override. Q3: What is a fully qualified name?Answer: The complete package + class name, e.g.,java.util.ArrayList. Used when import is not available or when disambiguating same-named classes. Q4: Can you access a protected field of a parent class from a subclass in a different package?Answer: Yes, if accessed through an expression of the subclass's type (inheritance). No, if accessed through an expression of the parent's type (package restriction). Q5: Should you use wildcard imports (*) or explicit imports?Answer: Both work. Explicit imports are preferred for readability. Wildcard imports are fine for quick prototypes and don't impact performance. Q6: What is the default package? Should you use it?Answer: Classes withoutpackagedeclaration are in the "default package". Never use it for real projects — classes in default package can't be imported by classes in named packages. Q7: What is a static import?javaimport static java.lang.Math.*; double area = PI * r * r; // Instead of Math.PIAnswer: Static imports allow accessing static members (fields, methods) of a class without qualifying with the class name. Q8: Can you have a public class in a .java file named differently?Answer: No. The public class name MUST match the filename. A .java file can have only one public class, but can have many package-private classes.
📐 Key Concepts
| Modifier | Visibility | Used For |
|---|---|---|
private | Class only | Fields, internal helpers |
| default | Package | Implementation details within package |
protected | Package + subclasses | Inheritance hooks |
public | Everywhere | API, constants |
🔗 Cross-References
- Next: Iterators & Callbacks Join Discord Previous4.1 Abstract Classes & InterfacesNext5.2 Iterators & Callbacks