Skip to content
Ameen Abozaid
BEG-027

Packages and organization

  • 5 Lessons
  • 57 min of reading
  • 2 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Packages
  • imports
  • visibility
  • cohesive class design

Learn first

57 min of reading · 2 h of practice (estimate)

Learning outcomes

  1. Organize classes into packages that match folder structure and use imports correctly.
  2. Use package-private visibility to hide implementation classes.
  3. Group classes by feature rather than by technical type, and justify it.

Lessons

  1. 01Packages and folders12 min read
  2. 02Imports and name clashes11 min read
  3. 03Package-private: the forgotten modifier12 min read
  4. 04Package by feature vs by layer11 min read
  5. 05Demo: reorganizing Bank Accounts into feature packages11 min read

Lab and exercises

Lab

BEG-027-LAB1 · Bank Accounts v5

Outcomes: 1, 2, 3 · Time: ~1.5 h Reorganize Bank Accounts into feature packages (account, customer, notification) with a minimal public surface. Write two sentences justifying feature over layer packaging. Acceptance: only intended classes are public.

Exercises (assessment: refactoring exercise)

IDOutcomeTask
BEG-027-EX011Give the folder path and package line for class Invoice in package com.acme.billing.
BEG-027-EX021Fix the clash: your code needs both java.util.Date and java.sql.Date.
BEG-027-EX032A class RateTable is used only by Account. Which modifier should it have and why?
BEG-027-EX043Group these into packages by feature: OrderController, OrderService, OrderRepository, UserController, UserService.
BEG-027-EX051, 2, 3Refactoring exercise: take a provided project in the default package and reorganize it with a small public surface.
Show solutions
  • EX01 com/acme/billing/Invoice.java with package com.acme.billing;.
  • EX02 Import one (import java.util.Date;) and write java.sql.Date in full where needed.
  • EX03 Package-private (no modifier): only its package needs it, and hiding it keeps the public surface small.
  • EX04 order/ (the three Order classes) and user/ (UserController, UserService). Justification: related code together, helpers can be package-private.
  • EX05 Graded on: correct package lines and folders, compiles and runs, helpers package-private, no use of the default package.

Quiz

Outcomes: O1 packages and imports · O2 package-private · O3 feature vs layer.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1The package com.acme.app maps to the folder:O1
  2. 2The package statement must be:O1
  3. 3Which package needs no import?O1
  4. 4Two classes with the same simple name from different packages:O1
  5. 5A class declared with no access modifier is visible:O2
  6. 6A good reason to use package-private:O2
  7. 7The public surface of a package should be:O2
  8. 8Package by feature puts together:O3
  9. 9A benefit of feature packages:O3
  10. 10Using the default package for a real project is:O1

Practical part

Refactoring exercise BEG-027-EX05, the other exercises, and Bank Accounts v5. Pass: project compiles in feature packages and only intended classes are public.

Common mistakes

One giant default package; everything public.

Assessment

Refactoring exercise.

Career relevance

Modular monoliths (MID-059) start here.

Type to search levels, phases, modules and lessons.