Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Understanding 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

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review

 

 

Layer 1
Login Categories