Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Video lesson

Understanding the Package in Java

In this lesson, you will learn

  • Defining a Package
  • Advantages of Using Packages
  • Creating a Package
  • Importing a Package
  • Examples

 

Defining a Package

Packages in Java are used to group related classes, interfaces, sub-packages, and enumerations together.

This organization helps manage and maintain the code efficiently, prevents naming conflicts by categorizing classes into different namespaces, and controls access with visibility options.

A package works as a container for classes and other sub-packages, allowing a modular programming approach

 

Advantages of Using Packages

  • Namespace Management: Helps organize classes by functionality, making, locating, and using them easier.
  • Access Protection: Packages enable access control through public, private, protected, and default access modifiers, helping encapsulate the code.
  • Code Reusability: Packages promote code reuse through public classes and interfaces that can be used across different programs.

 

Creating a Package

Syntax

package pkg;

 

Here, pkg is the name of the package

 

Examples

package vehicles;

package vehicles.car; //subpackage

package com.jcodebook;

package com.jcodebook.blog;

 

A Simple package Hierarchy

 

Example 1: Understanding Package

Creating a package named ‘vehicles’

package vehicles;

public class Car {
	public void display() {
        System.out.println("This is a Car");
    }
}

 

Creating a sub-package named luxury inside vehicles

package vehicles.luxury;

public class LuxuryCar {
	 public void display() {
	        System.out.println("This is a Luxury Car");
	    }
}

 

Importing a Package

There are two ways of import statement:

import packagename.ClassName;   // To import a particular class only
import packagename.*;    // To import the whole package

 

The below examples show how to import a particular class from a package.

 

Example 1 Contd. : Importing a Package

import vehicles.Car; //Importing a class 'Car'
import vehicles.luxury.LuxuryCar; //Importing a class 'LuxuryCar'

public class AccessCar {
	public static void main(String[] args) {
		Car objCar=new Car();
		objCar.display();
		LuxuryCar objLuxuryCar=new LuxuryCar();
		objLuxuryCar.display();
	}
}

Output

This is a Car
This is a Luxury Car

 

Example 2: Understanding Package

Creating a package named ‘com.jcodebook.blog’

 

package com.jcodebook.blog;

public class Chat {
	public void message() {
		System.out.println("Blogging......");
	}
}

 

Creating another package named ‘com.jcodebook.platform’

 

package com.jcodebook.platform;

public class Chat {
	public void message() {
		System.out.println("Platform......");
	}
}

 

Example 2 Contd. : Importing a Class

package com.jcodebook;

import com.jcodebook.blog.Chat;
public class Apps {

	public static void main(String[] args) {
		Chat blogChat=new Chat();
		blogChat.message();
		
		com.jcodebook.platform.Chat platform=new com.jcodebook.platform.Chat();
		platform.message();
	}
}

Output

Blogging......
Platform......

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

5.0
5.0 out of 5 stars (based on 3 reviews)
Excellent100%
Very good0%
Average0%
Poor0%
Terrible0%

 

 

April 11, 2025

Helpful for understanding the concepts and examples gives more clarity.

April 5, 2025

Well understood and clear all the concepts

April 3, 2025

Best for indepth knowledge of java programming

 

 

Submit a Review

 

 

Layer 1
Login Categories