0Pricing
Java Academy · Lesson

Packages and Access Modifiers

Packages and Access Modifiers

Packages and Access Modifiers is a free Java Academy lesson on CoddyKit — lesson 1 of 1. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 1 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

Hello,

Java uses access modifiers to limit the access of objects between each other.

This can sometimes be at the class level as well as at the variable, method, or constructor level.

Before going into the access modifier concept details, we need to learn the concept of a package in Java.

Intro 2

In Java, we can group classes that do the same thing with each other. We can think of it as grouping files with similar tasks in the same file system into a folder.

The packaging is of great importance, especially in order not to conflict between classes and methods with the same name but with different functions.

Built-in packages

Let's examine Build-in packages now.

There is a fairly large pool of classes that come with the Java Development Environment in Java.

In this pool, we will find the classes that we will need while developing our application.

concrete example

We can think of it as a library. A library has shelves categorically divided and books on these shelves.

When we think of a concrete example in Java, the shelves in the library correspond to the packages and the books to the classes.

cont.

When we want to use a class in the library in one of our classes, we need to import either the whole package or the related class itself into our class.

Below is a sample import example used from Java API.

import package.name.*; // Import the whole package
import package.name.Class; // Import a single class

Syntax

When we look at syntax, after the import keyword, the class is imported directly in the code expressed with package.packageName.className, 

while package.packageName. * Used with an asterisk has made the appropriate classes in the package available to our Class.

import java.util.Scanner;

class Main {

    public static void main(String[] args) {

    Scanner myObj = new Scanner([System.in](http:/system.in/));
      
   System.out.println("Enter username");
   String userName = myObj.nextLine();              System.out.println("Username is: " + userName);

                }
            }

Access modifier

In case we do not define it in Java, it takes the default access modifier.

The access modifiers that we can define are as follows.

public

Public

As we can see from its name, the least restrictive among access modifiers are public.

The methods, classes, variables defined by public can be accessed from anywhere by the application.

default

Default

As soon as we do not use any access modifier, java assigns the default access modifier for us.

These access modifiers can only be accessed within the same package.

Warning

Classes can only have public or default access modifiers.

private

Private

The defined element can only be accessed in the relevant class.

It is inaccessible from the same package or subclass whose related class is extended.

Protected

Protected

The code defined as Protected can be accessed from the same package and by subclasses.

The most confusing issue here is the difference between the protected and default access modifier. While neither of them imposes any restrictions on the package level. Allows access to the relevant code in the protected subclasses.

The default access token does not allow the subclass to access the relevant code of the parent class in extend cases.

Congratulations

Congratulations 🎉

In this lesson, we learned about access modifiers in Java.

See you in the next lesson

Packages and Access Modifiers — illustration 13

Frequently asked questions

Is the “Packages and Access Modifiers” lesson free?

Yes — the full text of “Packages and Access Modifiers” is free to read here on the web, and the Java Academy course includes 1 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Packages and Access Modifiers”?

Packages and Access Modifiers You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 1, so you can start here or from the beginning and move at your own pace.

How long does the “Packages and Access Modifiers” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

← Back to Java Academy