Creating a Java Package - Hands-On Guide

Introduction

In my preceding article, we delved into the foundational understanding of Packages and their significance in Java programming. Now, building upon that knowledge, this article aims to provide a practical demonstration of the concepts discussed through a comprehensive case study.

By examining a real-world scenario, we will navigate through the implementation of packages in Java, showcasing their practical application and elucidating how they contribute to creating well-organized and efficient code structures. This hands-on exploration will not only reinforce the theoretical aspects but also equip you with valuable insights into effectively utilizing packages to enhance the design and functionality of your Java projects.

Create a Package called main to contain classes Main1, Subclass, and SamePackage. The Main1 class should contain declarations of variables with different access specifiers-without access specifier, private, public, private protected, and protected. Inside the constructor, print all the variables.

The SubClass is derived from the Main1 class.

The SubClass and SamePackage class should contain a constructor that tries to access the same variables given above. Check if this is possible.

  • Create a class called PackTest, which imports the above package and creates an object of class SamePackage.
  • Create a class called PackTest1, which also imports the same package but creates an object of class SubClass.

Source Code

package main;
public class SamePackage
{
public SamePackage()
{
System.out.println("=============================================================");
System.out.println("Because of creation of main1 class object inside SamePacjage class");
Main1 m=new Main1();
System.out.println("Inside Package Main- class SamePackage Constructor");
System.out.println("Cannot access private, protected variables");
System.out.println("=============================================================");
System.out.println("Without Specifier--" +m.i);
System.out.println("Public Variable-" +m.j);
System.out.println("Protected Variable-"+m.p);
System.out.println("=============================================================");
}
}

Create a directory called main and store the above program in a file called SamePackage.java

a). Create a class called PackTest, which imports the above package and creates an object of class SamePackage.

//Test to check package main
import main.*;
class PackTest
{
public static void main(String args[])
{
System.out.println("Inside PackTest class- to test  Package main");
System.out.println("=======================================");
SamePackage p=new SamePackage();
}
}

The above program puts into use the package main by importing it. This is to be stored in a file called PackTest.java in a directory other than main.

b). Create a class called PackTest1, which also imports the same package but creates an object of class SubClass.

package main;
public class Main1
{
int i=9;
private int k=10;
public int j=10;
protected int p=12;
protected int o=8;
public Main1(){
System.out.println("Inside Main Package- Main1 class constructor");
System.out.println("Accessibility of all Variables");
System.out.println("=====================================");
System.out.println("Without Specifier-"+i);
System.out.println("Private Variable -"+k);
System.out.println("Public Variable -"+j);
System.out.println("Protected Variable -"+p);
System.out.println("Protected Variable -"+o);
System.out.println("=====================================");
}
}

Save the above program as Main1.java in the main directory.

package main;
public class SubClass extends Main1
{
public SubClass()
{
System.out.println("Derived Class Constructor");
System.out.println("Cannot access Private Variable alone");
System.out.println("=================================");
System.out.println("Without Specifier - "+i);
System.out.println("Public Variable- "+j);
System.out.println("Protected Variable- "+p);
System.out.println("Protected Variable- "+o);
System.out.println("=================================");
}
}

The above program is stored as SubClass.java in the main directory.

import main.*;
class PackTest1
{
public static void main(String args[])
{
System.out.println("Inside PackTest1 class - to test main package");
SubClass s=new SubClass();
}
}

The above program is stored as PackTest1.java in a directory other than the main directory.

Output

Output

Summary

Packages in Java serve as a fundamental organizational mechanism, providing a structured way to manage and group related classes and interfaces. They play a pivotal role in the modular design of Java applications, aiding in the creation of scalable and maintainable codebases. Packages offer a means to encapsulate code, prevent naming conflicts, and enhance code readability by establishing a clear hierarchy. Furthermore, they facilitate the development of large-scale projects by allowing for the division of code into logical units, fostering collaboration among developers.

The use of packages not only promotes code reusability but also contributes to the overall efficiency and clarity of Java programming. In essence, packages are indispensable components of Java's architecture, empowering developers to build robust, well-organized, and easily maintainable software solutions.


Similar Articles