Assignment 6.java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Assignment 6

Objective: Create java program with the use of java packages.


Description: Package in Java is a mechanism to encapsulate a group of classes, sub
packages and interfaces. A package is a container of a group of related classes where
some of the classes are accessible are exposed and others are kept for internal
purpose. We can reuse existing classes from the packages as many time as we need it
in our program.
Packages are used for:
 Preventing naming conflicts. For example there can be two classes with name
Employee in two packages, college.staff.cse.Employee and
college.staff.ee.Employee
 Making searching/locating and usage of classes, interfaces, enumerations and
annotations easier
 Providing controlled access: protected and default have package level access
control. A protected member is accessible by classes in the same package and its
subclasses. A default member (without any access specifier) is accessible by
classes in the same package only.
 Packages can be considered as data encapsulation (or data-hiding).
Note: Java compiler imports java.lang package internally by default. It
provides the fundamental classes that are necessary to design a basic Java
program. The important classes are Object, which is the root of the class
hierarchy, and Class, instances of which represent classes at run time.

There are two types of packages in java:


1. User-defined Package (Create Your Own Package’s)
2. Built-in packages are packages from the java application programming
interface that are the packages from Java API for example such
as swing, util, net, io, AWT, lang, javax, etc.

Syntax: To import a package


import package.name.*;

Example: Java Program to Import a package

// Importing java utility package


import java.util.*;

// Main Class
class GFG {

// Main driver method


public static void main(String[] args)
{
// Scanner to take input from the user object
Scanner myObj = new Scanner(System.in);
String userName;

// Display message
// Enter Your Name And Press Enter
System.out.println("Enter Your Name:");

// Reading the integer age entered using


// nextInt() method
userName = myObj.nextLine();

// Print and display


System.out.println("Your Name is : " + userName);
}
}

Input
Enter Your Name:
student
Output
Your Name is : student

Now in order to create a package in java follow the certain steps as described
below:
1. First We Should Choose A Name For The Package We Are Going To
Create And Include. The package command In The first line in the java
program source code.
2. Further inclusion of classes, interfaces, annotation types, etc that is
required in the package can be made in the package. For example, the
below single statement creates a package name called “FirstPackage”.
Syntax: To declare the name of the package to be created. The package statement
simply defines in which package the classes defined belong.
package FirstPackage ;
Implementation: To Create a Class Inside A Package
1. First Declare The Package Name As The First Statement Of Our
Program.
2. Then We Can Include A Class As A Part Of The Package.
// Name of package to be created
package FirstPackage;

// Class in which the above created package belong to


class Welcome {
// main driver method
public static void main(String[] args)
{
// Print statement for the successful
// compilation and execution of the program
System.out.println(
"This Is The First Program Geeks For Geeks..");
}
}
Procedure:
1. To generate the output from the above program
Command: javac Welcome.java
2. The Above Command Will Give Us Welcome.class File.
Command: javac -d . Welcome.java
3. So This Command Will Create a New Folder Called FirstPackage.
Command: java FirstPackage.Welcome
Output: The Above Will Give The Final Output Of The Example Program

Example2: Name of package to be created


package data;

// Class to which the above package belongs


public class Demo {

// Member functions of the class- 'Demo'


// Method 1 - To show()
public void show()
{

// Print message
System.out.println("Hi Everyone");
}

// Method 2 - To show()
public void view()
{
// Print message
System.out.println("Hello");
}
}
Procedure:
1. To generate the output from the above program
Command: javac Demo.java
2. This Command Will Give Us a Class File
Command: javac -d . Demo.java
3. So This Command Will Create a New Folder Called data.
Note: In data Demo.java & Demo.class File should be present

You might also like