0% found this document useful (0 votes)
5 views16 pages

Lecture 9.2 - User Defined Package

This document outlines the concepts of user-defined packages and exception handling in Java, covering topics such as package creation, hierarchy, naming conventions, and access protection. It explains the importance of packages in organizing classes and preventing naming conflicts, as well as how to find and import packages using CLASSPATH. Additionally, it provides examples and references for further reading on Java programming.

Uploaded by

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

Lecture 9.2 - User Defined Package

This document outlines the concepts of user-defined packages and exception handling in Java, covering topics such as package creation, hierarchy, naming conventions, and access protection. It explains the importance of packages in organizing classes and preventing naming conflicts, as well as how to find and import packages using CLASSPATH. Additionally, it provides examples and references for further reading on Java programming.

Uploaded by

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

Package & Exception

Handling
Course Code: CSC 1205 Course Title: Object Oriented Programming 1

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 19 Week No: 10 Semester: Summer 19-20


Lecturer: Md. Mahmudur Rahman ([email protected])
Lecture Outline

1. Introduction to User Defined Package


2. Create a Package
3. Hierarchy of packages
4. Naming Convention
5. Finding Packages and CLASSPATH
6. Import Packages
Introduction to User Defined
Package
What is Package

 Packages are a feature of the Java programming language that help us to


organize and structure our classes and their relationships to one another.
 In other words, a package is a grouping of related types providing access
protection and name space management.
 This is the general form of the package statement:

 There are 2 types of packages:


 Built-in package: Already defined packages like java.io.*, java.lang.* etc.
are known as built-in packages.
 User defined package: The package we create is called user-defined
package.
Introduction to User Defined
Package
Why Packages are used

 Preventing naming conflicts.


 Making searching/locating and usage of classes, interfaces, enumerations and
annotations easier.
 Providing controlled access.
 Packages can be considered as data encapsulation (or data-hiding).
Introduction to User Defined
Package
Access Protection

 Java’s access control mechanism may seem complicated, we can simplify it as


follows.
Private No Modifier Protected Public

Same Class Yes Yes Yes Yes

Same package subclass No Yes Yes Yes

Same package non-subclass No Yes Yes Yes

Different package subclass No No Yes Yes

Different package non-subclass No No No Yes


Create a Package
Package Creation Process

 To create a package, choose a name for the package and put a package
statement with that name at the top of java source file.
 The package statement must be the first line in the source file.
 There can be only one package statement in each source file.

 If you do not use a package statement, your type ends up in an unnamed


package
Create a Package
Directories

 Java uses file system directories to store packages.


 For example, the .class files for any classes you declare to be part of
MyPackage must be stored in a directory called MyPackage.
 From last example we can say that we have to put our HelloWorld.class file in
greeting directory.
D:\greeting
HelloWorld.java
Hierarchy of packages
Example of Hierarchy of Packages

 We can create a hierarchy of packages.


 To do so, simply separate each package name from the one above it by use of
a period.
 The general form of a multileveled package statement is shown here:
Hierarchy of packages
Hierarchy

D:\demo\greeting
HelloWorld.java
Naming Convention
Package Naming Convention

 The prefix of a unique package name is always written in all-lowercase ASCII


letters and should be one of the top-level domain names.
 Subsequent components of the package name vary according to an
organization's own internal naming conventions
 For Example:
com.sun.eng
com.apple.quicktime.v2
edu.cmu.cs.bovik.cheese
Finding Packages and
CLASSPATH
Finding Packages and CLASSPATH

 As we know, packages are mirrored by directories, so how does the Java run-
time system know where to look for packages that we create.
 The answer has three parts:
 First, by default, the Java run-time system uses the current working
directory as its starting point.
 Second, you can specify a directory path or paths by setting the
CLASSPATH environmental variable.
 Third, you can use the -classpath option with java and javac to specify
the path to your classes.
 Let’s take an example.
Finding Packages and
CLASSPATH
Example Finding Packages and CLASSPATH

 Let’s take our previous code. And our HelloWorld.java file location is: D:\
MyProgram\Java\greeting

 First, we must go above to greeting folder. Which is D:\MyProgram\Java. Now


we can compile with the following command.

javac greeting/HelloWorld.java

 It will create class file in the greeting directory, Next, we can run the file using
the following command.
java greeting/HelloWorld.java
Import Packages
How to import package

 Java includes the import statement to bring certain classes, or entire


packages, into visibility.
 Once imported, a class can be referred to directly, using only its name.
 This is the general form of the import statement:

 In a Java source file, import statements occur immediately following the


package statement (if it exists) and before any class definitions.
 In the above syntax, pkg1 is the name of a top-level package, and pkg2 is the
name of a subordinate package inside the outer package separated by a dot
(.).
 Finally, specify either an explicit classname or a star (*), which indicates that
the Java compiler should import the entire package
Import Packages
Example of how to import package
Books

1. Java The Complete Reference- Ninth Edition by Herbert Schildt


References

• https://docs.oracle.com/javase/tutorial/

You might also like