0% found this document useful (0 votes)
18 views

PACKAGES

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

PACKAGES

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Packages

A java package is a group of similar types of classes, interfaces and sub-packages. All
we need to do is put related classes into packages. After that, we can simply write an
import class from existing packages and use it in our program. 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.

How packages work?:

Package names and directory structure are closely related. For example if a package
name is college.staff.bse, then there are three directories, college, staff and bsc such
that bsc is present in staff and staff is present inside college. Also, the directory
college is accessible through CLASSPATH variable, i.e., path of parent directory of
college is present in CLASSPATH. The idea is to make sure that classes are easy to
locate.
Package naming conventions : Packages are named in reverse order of domain
names, i.e., org.college.practice. For example, in a college, the recommended
convention is college.tech.bsc, college.tech.bcom, college.art.bcca, etc.

Adding a class to a Package : We can add more classes to a created package by


using package name at the top of the program and saving it in the package directory.
We need a new java file to define a public class, otherwise we can add the new class
to an existing .java file and recompile it.

Subpackages: Packages that are inside another package are the subpackages.
These are not imported by default, they have to imported explicitly. Also, members of
a subpackage have no access privileges, i.e., they are considered as different package
for protected and default access specifiers.

Advantage of Package

OBJECT ORIENTED PROGRAMMING THROUGH JAVA 1


1) Java package is used to categorize the classes and interfaces so that they can be
easily maintained.

2) Java package provides access protection.

3) Java package removes naming collision.

How to compile java package


If you are using any IDE then IDE will compile and produce output to you, But you
are not using any IDE, you need to follow the syntax given below.

Syntax:

javac -d directory javafilename

Example:
To Compile: javac -d . Simple.java
To Run: java mypack.Simple

OBJECT ORIENTED PROGRAMMING THROUGH JAVA 2


The -d switch specifies the destination where to put the generated class file. You can
use any directory name like /home (in case of Linux), d:/abc (in case of windows)
etc. If you want to keep the package within the same directory, you can use . (dot).

Another Way:
1. Create a folder with same as package name and save the package program.
2. Compile the program as javac class_name.java
3. Package folder and main java class file put it into the single folder.
4. Then compile the main java program and run the java program.
Compile: javac main_class.java
Run: java main_class

Types of packages:
Package in java can be categorized in two form, built-in package and user-defined
package.

Built-in Packages:

These packages consist of a large number of classes which are a part of Java API.
Some of the commonly used built-in packages are:
1) java.lang: Contains language support classes(e.g classed which defines primitive
data types, math operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes which implement data structures like Linked
List, Dictionary and support ; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for graphical user
interfaces (like button , ;menus etc).
6) java.net: Contain classes for supporting networking operations.

OBJECT ORIENTED PROGRAMMING THROUGH JAVA 3


User-defined packages:

These are the packages that are defined by the user. First we create a directory
myPackage (name should be same as the name of the package). Then create the
MyClass inside the directory with the first statement being the package names.

Example:

// Name of the package must be same as the directory


// under which this file is saved
package myPackage;
public class MyClass{
public void getNames(String s){
System.out.println(s);
}
}
Now we can use the MyClass class in our program.

/* import 'MyClass' class from 'names' myPackage */


import myPackage.MyClass;
public class PrintName {
public static void main(String args[]) {
String name = "Java package";

// Creating an instance of class MyClass in


// the package.
MyClass obj = new MyClass();
obj.getNames(name);
}
}
Creating a package and accessing a package:

The package keyword is used to create a package in java.

OBJECT ORIENTED PROGRAMMING THROUGH JAVA 4


Syntax:
Package package_name;
// class definition
Example:
Package mypack;
Public class Simple{
Public static void main(String[] args){
Systeml.out.println(“Welcome to java”);
}
}
When ever you save the package file the package name and folder name must be the
same. Above example Simple.java compiled as
Compile: javac -d .\mypack Simple.java
Run: java mypack.Simple
Accessing a package:
There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
Using packagename.*
If you use package.* then all the classes and interfaces of this package will be
accessible but not subpackages.
The import keyword is used to make the classes and interface of another package
accessible to the current package.
Example:
package pack;
public class A{
public void msg(){System.out.println("Hello Package");}
}

OBJECT ORIENTED PROGRAMMING THROUGH JAVA 5


package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:
Hello Package
Using packagename.classname
If you import package.classname then only declared class of this package will be
accessible.
Example
A.java
package pack;
public class A{
public void msg(){System.out.println("Hello Package");}
}

B.java
package mypack;
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:

OBJECT ORIENTED PROGRAMMING THROUGH JAVA 6


Hello Package
Using fully qualified name
If you use fully qualified name then only declared class of this package will be
accessible. Now there is no need to import. But you need to use fully qualified name
every time when you are accessing the class or interface. It is generally used when
two packages have same class name e.g. java.util and java.sql packages contain Date
class.
Example
A.java
package pack;
public class A{
public void msg(){System.out.println("Hello Package");}
}

B.java
package mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();//using fully qualified name
obj.msg();
}
}
Output:
Hello Package
Subpackage in java
Package inside the package is called the subpackage. It should be created to
categorize the package further. Let's take an example, Sun Microsystem has definded
a package named java that contains many classes like System, String, Reader, Writer,
Socket etc. These classes represent a particular group e.g. Reader and Writer classes
are for Input/Output operation, Socket and ServerSocket classes are for networking

OBJECT ORIENTED PROGRAMMING THROUGH JAVA 7


etc and so on. So, Sun has subcategorized the java package into subpackages such as
lang, net, io etc. and put the Input/Output related classes in io package, Server and
ServerSocket classes in net packages and so on.

Example of Subpackage
package com.java.core;
class Simple{
public static void main(String args[]){
System.out.println("Hello subpackage");
}
}
To Compile: javac -d . Simple.java
To Run: java com.java.core.Simple

Important points:

1. Every class is part of some package.


2. If no package is specified, the classes in the file goes into a special unnamed
package (the same unnamed package for all files).
3. All classes/interfaces in a file are part of the same package. Multiple files can
specify the same package name.
4. If package name is specified, the file must be in a subdirectory called name (i.e.,
the directory name must match the package name).
5. We can access public classes in another (named) package using:
package-name.class-name
Streams:
A stream is a sequence of data. In Java, a stream is composed of bytes. It's called a
stream because it is like a stream of water that continues to flow. Java I/O (Input
and Output) is used to process the input and produce the output.Java uses the
concept of a stream to make I/O operation fast. The java.io package contains all the
classes required for input and output operations.

OBJECT ORIENTED PROGRAMMING THROUGH JAVA 8


Java provides I/O stream to read and write data where, a stream represents an input
source or an output destination which could be a file, i/o device other program.

A stream will be an input stream or an output stream

● InputStream – This used to read data from a source


● OutputStream – This is used to write data to a destination.

Based on the data handling there are two types of streams

Byte Streams: These handle data in bytes i.e the byte stream classes read/write
data. Using these you can store characters, videos, audios, images .. etc.

Character Streams – These handle data in Unicode. Using these you can read and
write text data only.

OBJECT ORIENTED PROGRAMMING THROUGH JAVA 9


There are three streams are created for us automatically. All these streams are attached with the console.

1) System.out: standard output stream


2) System.in: standard input stream
3) System.err: standard error stream
working of Java OutputStream and InputStream

OBJECT ORIENTED PROGRAMMING THROUGH JAVA 10

You might also like