Core Java · Chapter 16 of 48

Packages and access modifiers

By the end of this chapter you can put classes in packages, use a class from another package, and choose exactly who may see each class, field and method. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Objects
  • 5 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Core Java are free. This one comes with the all labs plan.

Why you need this

Tapri Counter has grown to a dozen classes in one folder, and two people have written a class called Report. Kabir wants three groups: tapri.menu for the menu, tapri.billing for the till and combo rules, tapri.station for station-specific code. Meera has her own reason: combo offers change weekly, so only billing may depend on them; if menu code used them, every change would break something.

Until now every class lived in one default package, where anything without a modifier is visible to all the others. Nothing could say "billing only", and two classes cannot share a name.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Menu.java: look up a price and refuse unknown items

    In Menu.java, make basePrice(item) look the item up in the price list and return its price in rupees; priceOf(item), which calls it, then returns the same price. For an item not on the menu, throw an IllegalArgumentException with exactly the message "Not on the menu: " followed by the item.

    For example, new Menu().priceOf("Masala chai") returns 40, "Cutting chai" 25 and "Bun maska" 50, and priceOf("Samosa") throws an IllegalArgumentException with the message Not on the menu: Samosa.

  2. Task 2

    Till.java: add up an order, from another package

    In Till.java, in the package tapri.billing, make subtotal(items) return the sum of the menu price of every item, asking the Menu the till was given (imported from tapri.menu) through its priceOf. With no items it returns 0, and an item counts as often as it appears. The prices come from task 1.

    For example, with new Menu(), subtotal() is 0, subtotal("Masala chai", "Bun maska") is 90, and subtotal("Masala chai", "Masala chai", "Cutting chai") is 105.

  3. Task 3

    Menu.java: hide the price list with private

    In Menu.java, make the prices field private, so only Menu's own code can read or change the price list. Everything else keeps working, because only Menu's own constructor and methods use it.

    For example, after the change new Menu().priceOf("Bun maska") still returns 50.

  4. Task 4 · interview question

    Interview: Discount.java, keep the combo rules inside billing

    In Discount.java, make the class Discount and its method comboOff package-private, with no access modifier at all, so only classes in tapri.billing can use them. Then, in Till.java (the same package), make total(items) return the subtotal minus Discount.comboOff of the items as a list: comboOff already gives ₹10 off when the order has both a masala chai and a bun maska, and 0 otherwise.

    For example, total("Masala chai", "Bun maska") is 80 (90 minus the combo), total("Masala chai", "Cutting chai") is 65, and total("Masala chai", "Bun maska", "Masala chai", "Cutting chai") is 145 (155 minus one combo).

  5. Task 5

    StationMenu.java: reach basePrice from another package with protected

    In Menu.java, make basePrice(item) protected instead of private, so StationMenu, a subclass of Menu in the package tapri.station, can call it. Then, in StationMenu.java, make priceOf(item) return the base price of the item plus this station's surcharge. An item not on the menu still throws an IllegalArgumentException.

    For example, new StationMenu(5).priceOf("Masala chai") returns 45 and "Cutting chai" 30, priceOf("Samosa") throws, and a Till built with that station menu gives a subtotal of 100 for a masala chai and a bun maska.

What you will learn

  • package and import: give a class a full name, and use classes from other packages.
  • Where the files live: what javac, the tools and the lab expect.
  • The four access levels: private, package-private (no modifier), protected and public.

More in Objects

Build "Packages and access modifiers" yourself

Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Core Java are free. This one comes with the all labs plan.