Final Unit-IV
Final Unit-IV
Contents:-
Defining a Package
Important Packages
Access protection
importing packages
1
Packages:
Package:
A package is a container of classes and interfaces. A package represents a directory that
contains related group of classes and interfaces.
For example, when we write statements like:
import java.io.*;
Here we are importing classes of java.io package. Here, java is a directory name and io is
another sub directory within it. The ‘*’ represents all the classes and interfaces of that io
sub directory.
We can create our own packages called user-defined packages or extend the available
packages. User-defined packages can also be imported into other classes and used exactly in
the same way as the Built-in packages. Packages provide reusability.
General form for creating a package: package packagename; e.g.: package pack;
The first statement in the program must be package statement while creating a package
2
Packages:
Package:
While creating a package except instance variables, declare all the members and the class
itself as public then only the public members are available outside the package to other
programs.
Note: Sequence of the program must be package then import then class.
3
Packages:
Package:
Another way to run this program by -classpath switch of java:
The -classpath switch can be used with javac and java tool.
The –d option tells the Java compiler to create a separate directory and place the .class file in
that directory (package). The (.) dot after –d indicates that the package should be created in
the current directory. So, package pack with Addition class is ready.
4
Packages:
Package:
Program 2: Write a program to use the Addition class & Subtraction class of package pack.
Code-3:
Program 4: Write a program to access all the classes in the package pack.
To import all the classes and interfaces in a class using import pack.*;
Code-4:
Note : In this case, please be sure that any of the Addition.java and Subtraction.java
programs will not exist in the current directory. Delete them from the current directory as
they cause confusion for the Java compiler. The compiler looks for byte code in Addition.java
and Subtraction.java files and there it gets no byte code and hence it flags some errors.
5
Packages:
Creating sub Package:
Here, we are creating pack2 subpackage which is created inside pack1 package. To use the
classes and interfaces of pack2, we can write import statement as:
import pack1.pack2;
Code-5:
6
Packages:
Access Specifier: Specifies the scope of the data members, class and methods.
private members of the class are available within the class only.
default members of the class are available within the class, outside the class and in its sub
class of same package and also available to subclasses in different package also.
7
Packages:
Code-6
Code-7
Code-8
8
Wrapper Class:
The variable of normal data type are send to any method are sent by Value. Hence, No two
methods in a memory can refer same instance of normal variable in memory. But sometimes it
becomes necessary that we want multiple methods to refer the same instance of variable in
memory. For this purpose we need to convert a variable of normal data type to object, this is
done by wrapper class. Wrapper class wraps the normal data type and convert them to object.
Wrapper class in java provides the mechanism to convert primitive into object and object
into primitive.
•Autoboxing and Unboxing feature converts primitive into object and object into
primitive automatically. The automatic conversion of primitive into object is known as
Autoboxing and vice-versa Unboxing.
Hierarchy :
10
Wrapper Class:
Number Class:
short shortValue();
int intValue();
long longValue();
float floatValue();
double doubleValue();
11
Wrapper Class:
Method :
int intValue();
Method
char charValue();
Method
boolean booleanValue();
12
Wrapper Class:
Code-9:
Code-10:
13
Wrapper Class:
Built-in Packages in Java:
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.
3) java.util: Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.
5) java.awt: Contain classes for implementing the components for graphical user
interfaces (like button , ;menus etc).