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

Java Module 4

This document provides an overview of Java packages, including built-in and user-defined packages, and their advantages such as access protection and organization of classes. It explains access control levels for class members and the use of the import statement to access classes from different packages. Additionally, it illustrates how to import packages and use fully qualified names to avoid naming collisions.

Uploaded by

ahavish9924
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Module 4

This document provides an overview of Java packages, including built-in and user-defined packages, and their advantages such as access protection and organization of classes. It explains access control levels for class members and the use of the import statement to access classes from different packages. Additionally, it illustrates how to import packages and use fully qualified names to avoid naming collisions.

Uploaded by

ahavish9924
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

MODULE 4 PACKAGES

MODULE 4

❖ PACKAGES: PACKAGES AND MEMBER ACCESS


❖ IMPORTING PACKAGES.
PACKAGES ❖ EXCEPTION-HANDLING FUNDAMENTALS
EXCEPTIONS ❖ EXCEPTION TYPES
❖ UNCAUGHT EXCEPTIONS
❖ USING TRY AND CATCH,
❖ MULTIPLE CATCH CLAUSES
❖ NESTED TRY STATEMENTS
❖ THROW, THROWS, FINALLY
❖ JAVA’S BUILT-IN EXCEPTIONS
❖ CREATING YOUR OWN EXCEPTION
SUBCLASSES
❖ CHAINED EXCEPTIONS.

EPCET Page 1
MODULE 4 PACKAGES

Packages:
❖ A java package is a group of similar types of classes, interfaces and sub-packages.
❖ Keyword is package
❖ 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.

EPCET Page 2
MODULE 4 PACKAGES
User-defined packages
➢ packages that are defined by the user.
➢ keyword called “package” which is used to create user-defined packages
Ex: package Package name;
❖ Advantages:
➢ Java package is used to categorize the classes and interfaces so that they can be
easilymaintained
➢ Making searching/locating and usage of classes easier
➢ Java package provides access protection.
➢ Java package removes naming collision.
➢ Packages can be considered as data encapsulation
Here, we will have the detailed learning of creating and using user-defined packages.
Simple Package example:
package mypack ; //keyword package followed by name of the package

Access Protection:
➢ Java provides many levels of control over the visibility of variables and methods within
classes, subclasses, and packages.
➢ Classes and packages are both means of encapsulating and containing the name space and
scope of variables and methods.
➢ Packages act as containers for classes and other subordinate packages.
➢ Classes act as containers for data and code.

Java addresses four categories of visibility for class members:

• Subclasses in the same package

• Non-subclasses in the same package

• Subclasses in different packages

EPCET Page 3
MODULE 4 PACKAGES
• Classes that are neither in the same package nor subclasses

➢ The three access specifiers, private, public, and protected, provide a control way of
access .
➢ Anything declared public can be accessed from anywhere.
➢ Anything declared private cannot be seen outside of its class.

➢ When a member does not have an explicit access specification(default access), it is


visible to subclasses as well as to other classes in the same package.

private No Modifier protected public

Same class YES YES YES YES


Subclasses in
the same NO YES YES YES
package

Non-subclasses
in the same NO YES YES YES
package

Subclasses in
different NO NO YES YES
packages

Classes that are


neither in the NO NO NO YES
same package
nor subclasses

Table: Class Member Access

Importing Packages:
➢ The import keyword used to import built-in and user-defined packages.
➢ When a package has imported, we can refer to all the classes of that package using their
name directly.
➢ The import statement must be after the package statement, and before any other
statement.

EPCET Page 4
MODULE 4 PACKAGES

➢ Using an import statement, we may import a specific class or all the classes from a
package.

Let us discuss how to access package from an other package (Access Protection)

There are 3 ways to access the package from outside the package.

1. import package.*;
2. import package.classname;
3. fully qualified name.

import package.*
If we use package.* then all the classes and interfaces of this package will be accessible.

The import keyword is used to make the classes and interface of another package
accessible to the current package.

package mypack1; //save One.java in the package mypack1

public class One


{
public void display()
{
System.out.println("Hello Java from mypack1");
}
}

package mypack2; //save Two.java in the package mypack2

import mypack1.*; //importing mypack1 all classes, interfaces ect.,


public class Two
{
public static void main(String[] args) Output:
{ Hello Java from mypack1
One obj=new One();
obj.display();
}
}

import package.classname
If you import package.classname then only declared class of this package will be accessible.

EPCET Page 5
MODULE 4 PACKAGES
package mypack1; //save One.java in the package mypack1

public class One


{
public void display()
{
System.out.println("Hello Java from mypack1");
}
}

package mypack2; //save Two.java in the package mypack2

import mypack1.One; //importing mypack1 class One only Output:


public class Two Hello Java from mypack1
{
public static void main(String[] args)
{
One obj=new One();
obj.display();
}
}

Fully Qualified name


➢ If we use fully qualified name then only declared class of this package will be
accessible. Now there is no need toimport. But we need to use fully qualified name
every time when we are accessing the class or interface.
➢ It is generally used when two packages have same class name.
package mypack1; //save One.java in the package mypack1

public class One


{
public void display()
{
System.out.println("Hello Java from mypack1");
}
}

package mypack2; //save Two.java in the package mypack2

public class Two


{
public static void main(String[] args)
{
mypack1.One obj=new mypack1.One (); //using name of the package followed by class name
obj.display();
}
}
SJCIT Page 6

You might also like