How to Create Different Packages For Different Classes in Java?
Let us first know what is a class and package in Java. Class in java is a model for creating objects. It means that the properties and actions of the objects are written in class. Properties are represented by variables and actions of the objects are represented by methods. So, a class contains variables and methods. The same variables are also available in the objects because they are created from the class. These variables are also called ‘instance variables’ because they are created inside the object (instance).
Illustration:
class Human { // Properties --> Instance variables String name; int age; // Actions --> methods void speak() { System.out.println("Hey there! I am " + name); System.out.println("My age is " + age); } }
Output:
Hey there! I am Vasanth Kumar My age is 20
Note: Here the keyword class is used to declare a class. After this, we should write a class name. In the class, we write instance variables and methods.
Packages in Java
In Java, Packages are a collection of classes, sub-packages, and interfaces. i.e A package represents a dictionary that contains a related group of classes and interfaces. Whenever we write a statement be it as shown below we are importing all the classes of the java.io.* package. Here, java is a directory name and io is another sub-directory within it. And the * represents all the classes and interfaces of that io subdirectory.
import java.io.*;
We have many such packages, for example, java.lang, java.util, and there do exists many more inside which there do lies classes inside the package. In order to use so let us do take most frequently used packages that are ‘lang’ package for java designing and ‘io’ package for input-output operations.
Let us discuss the advantages of packages in java that are as follows:
- Packages are useful to arrange related classes to interfaces into a group. This puts together all the classes and interfaces performing the same task in the package. For example, In Java, all the classes and interfaces which perform input and output operations are stored in the java.io package.
- Packages hide the classes and interfaces in a separate subdirectory.
- The classes and interfaces of a package are isolated from the classes and interfaces of another package.
There are two types of packages in java as listed:
- Type 1: Built-in packages
- Type 2: User-defined packages
Type 1: Built-in Packages
These are the packages that are already available in the Java language. These packages provide nearly all necessary classes, interfaces, and methods for the programmer to perform any task. They are as follows:
java.lang java.util java.io java.awt java.net java.applet java.text java.sql javax.swing
Type 2: User-defined Packages
Just like the built-in packages shown earlier, the user of the java language can also create their own packages. They are called User Defined Packages. These packages can also be imported into other classes and used exactly in the same way as built-in packages.
Syntax: Creating a package, package keyword is used
2.1.1 To create a package
package package_name ;
2.1.2 To create a subpackage within a package
package package_name.subpackagename ;
2.2 Compile
C:\> javac -d . classname.java
2.3 To run the program
C:\> java packagename.classname
Above syntax is valid only for windows CMD shell, for Linux and macOS zsh shell refer to below media as perceive it the same way provided below
Implementation:
Now let us divide the class into packages
Step 1: Define a class StudentData that holds the following information of a student:
- ID: It is a string that stores the unique ID of each student
- Name: It is a string indicating the name of the student.
Note that these fields should be declared private.
Step 2: Create another class StudentDataExtended with a private attribute named location. It is basically a string that stores the location of the student.
Step 3: Now in this class define a method addDetails() method that stores the details of students; and a method printDetails() method that outputs the details of students in the sorted order of their id.
Note: Both the classes should be in different packages
Illustration:
Enter the number of students : 2 Enter the details of Student 1 (id, name, location): B200 Ajay Hyderabad Enter the details of Student 2 (id, name, location): B100 Ramesh Hyderabad
Output:
The Student Details are: B100 Ramesh Hyderabad B200 Ajay Hyderabad
Implementation:
Now let us come up with real runnable examples to implement what we discussed and illustrated above:
Example 1-A:
Java
// Java Program Illustrating Dividion of Classes into // Packages where Class StudentData // which creates first package i.e pack1 // Importing package package pack1; // Main class public class StudentData { private String id; private String name; // Method 1 public void addStudentData(String id, String name) { // This keyword refers to current instance itself this .id = id; this .name = name; } // Getter setters Methods // Use getter methods so that we can // access private members for other packages public String getId() { return id; } public String getName() { return name; } } |
Example 1-B:
Java
// Java Program Illustrating Dividion of Classes into // Packages where Class StudentDataExtended creates // Second package- pack2 and uses the first package // Importing packages package pack2; // Importing required classes // from pre-defined packages import java.io.*; import java.lang.*; import java.util.*; import pack1.*; // Main class class StudentDataExtended extends StudentData { private String location; public void addDetails(String id, String name, String location) { addStudentData(id, name); // This keyword refers to current object itself this .location = location; } // Method 1 public static void printDetails(TreeMap<String, StudentDataExtended> map) { // Iterating via for each loop for (String a : map.keySet()) { StudentDataExtended student = map.get(a); // Print ID and name of student System.out.println(student.getId() + " " + student.getName() + " " + student.location); } } // Method 2 // Main driver method public static void main(String[] args) { // Display a message for asking input from the user System.out.print( "Enter the number of students : " ); // Scanner class to read user input Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String buffer = sc.nextLine(); int count = 1 ; // Creating a TreeMap TreeMap<String, StudentDataExtended> map = new TreeMap<>(); while (n != 0 ) { System.out.println( "Enter the details of Student " + count + " (id, name, location):" ); count++; String details = sc.nextLine(); String studentInfo[] = details.split( " " ); StudentDataExtended student = new StudentDataExtended(); student.addDetails(studentInfo[ 0 ], studentInfo[ 1 ], studentInfo[ 2 ]); map.put(studentInfo[ 0 ], student); n--; } // Display message System.out.println( "\nThe Student Details are:" ); // Calling above method 1 to // print details of the students printDetails(map); } } |
OUTPUT:
Please Login to comment...