OOP Chapter 5

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 11

Chapter 5

Packages
1 10/07/22
Introduction
 A Package can be defined as a grouping of related types
(classes, interfaces, enumerations and annotations )
providing access protection and namespace management.
 Think of it as a folder in a file directory.
 We use packages to prevent naming conflicts, to control
access, to make searching/locating and usage of classes,
interfaces, enumerations and annotations easier, and to
write a better maintainable code.
 Packages are divided into two categories:
 Built-in Packages (packages from the Java API)
 User-defined Packages (create your own packages)
2 10/07/22
Advantage of Java Package
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.

3 10/07/22
Built-in Packages
 The Java API is a library of prewritten classes, that are
free to use, included in the Java Development
Environment.
 The library contains components for managing input,
database programming, and much more.
 The library is divided into packages and classes. Meaning
you can either import a single class (along with its
methods and attributes), or a whole package that contain
all the classes that belong to the specified package.
 To use a class or a package from the library, you need to
use the import keyword:

4 10/07/22
User-defined Packages
 The user-defined packages are the packages created by the
user.
 User is free to create their own packages.
 Users can define their own packages to bundle group of
classes/interfaces, etc. It is a good practice to group
related classes implemented by you so that a programmer
can easily determine that the classes, interfaces,
enumerations, and annotations are related.
 Since the package creates a new namespace there won't be
any name conflicts with names in other packages. Using
packages, it is easier to provide access control and it is
also easier to locate the related classes.

5 10/07/22
The import Statement
 Import statement in Java is helpful to take a class or all
classes visible for a program specified under a package,
with the help of a single statement.
 It is pretty beneficial as the programmer do not require to
write the entire class definition.
 Hence, it improves the readability of the program.
 Syntax 1:
import package1[.package2].(*); 
 package1: Top-level package
 package2: Subordinate-level package under package1
 *: To import all the classes

6 10/07/22
The import Statement-2
 Syntax 2:
 import package1[.package2].(myClass);Here,
 package1: Top-level package
 package2: Subordinate-level package under the top-level
package
 myClass: Import only myClass 

7 10/07/22
Static Imports
 By using static import statements in Java, we can access
static member of a class directly without the use of a
fully-qualified name.
 Advantages:
 Import statements help in reducing the code size and
hence save a lot of time.
 It improves the readability of our code.
 It is pretty useful while handling big projects. 
 They can be used to combine the functionality of several
classes into one.

8 10/07/22
CLASSPATH and Import
 An import statement tells the compiler the path of a class
or the entire package.
 It is unlike “#include” in C++, which includes the entire
code in the program. Import statement tells the compiler
that we want to use a class (or classes) that is defined
under a package.
 It is pretty helpful and recommended over the “fully-
qualified name” method as it reduces the overall code size
and improves the source code’s readability.

9 10/07/22
Defining Packages
 In java, a package is a container of classes, interfaces, and
sub-packages.
 The user-defined packages are the packages created by the
user. User is free to create their own packages.
 We use the package keyword to create or define a
package in java programming language.
 Syntax package packageName;

10 10/07/22
Package Scope
 If you have a variable or method in your class that you don’t
want clients of your class directly accessing, don’t give it a
public, protected or private declaration.
 Due to an oversight in the design of Java, you can’t explicitly
declare the default package accessibility.
 Other members of the package will be able to see it, but classes
outside the package that inherit from yours, won’t.
 The protected accessibility attribute offers slightly more
visibility.
 A protected method is visible to inheriting classes, even not
part of the same package.
 A package scope (default) method is not. That is the only
difference between protected and package scope.

11 10/07/22

You might also like