0% found this document useful (0 votes)
0 views28 pages

Java Packages & Apis

The document provides an overview of the Java API and packages, explaining their importance in object-oriented programming. It details the organization of Java APIs into packages to manage namespaces and avoid naming conflicts, as well as the types of built-in and user-defined packages. Additionally, it covers how to create and use packages, naming conventions, and methods for importing package members.
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)
0 views28 pages

Java Packages & Apis

The document provides an overview of the Java API and packages, explaining their importance in object-oriented programming. It details the organization of Java APIs into packages to manage namespaces and avoid naming conflicts, as well as the types of built-in and user-defined packages. Additionally, it covers how to create and use packages, naming conventions, and methods for importing package members.
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/ 28

RUCU

OBJECT ORIENTED
PROGRAMMING I RCS 122

Vincent Pius Bob


Ruaha Catholic University
Department of Computer Science
Faculty of Information and Communication
Technology
05/22/2025 1
JAVA API &
PACKAGES
Java API is a library
of pre-written classes
and interfaces.
05/22/2025 2
Java API
 The Java API (Application Programming Interface) is an
extensive collection of pre-written classes and interfaces. Think
of it as a ready-made toolkit filled with functionalities you can
use in your Java programs.
 A Java API is a critical bridge between different applications,
allowing them to efficiently communicate and perform complex
tasks.
 Instead of writing code from scratch for common tasks, you can
leverage the existing API components.
 It is organized into Packages/Logical Grouping: The API is
organized into packages, which are like folders containing
related classes and interfaces. Namespace Management:
Packages help avoid naming conflicts between different classes.
05/22/2025 3

API concept
General API (Communication Between Software)
 This is the broader definition.
 APIs define how different software systems talk to
each other (e.g., REST APIs, SOAP).
 Example: Twitter's API lets your app fetch tweets
over HTTP.
Java API (Predefined Libraries)
 In Java, "API" refers to the built-in classes/methods
provided by Java itself.
 These are called APIs because they define how your
code interacts with Java’s prewritten functionality.
05/22/2025 4
Packages
To make types easier to find and use, to avoid
naming conflicts, and to control access,
programmers bundle groups of related types into
packages.
Definition: A package is a grouping of related
types providing access protection and name space
management. Note that types refers to classes,
interfaces, enumerations, and annotation
types.
Enumerations and annotation types are special
kinds of classes and interfaces, respectively,
05/22/2025 5
so types are often referred to in this lesson simply
Packages cont..
 Packages in Java act as namespaces to avoid naming
collisions between classes, interfaces, and methods. Here’s
how they work:
 Unique Naming with Package Hierarchy
 A package is a folder-like structure (e.g.,
com.company.project).
 Even if two classes have the same name, their fully
qualified names differ due to their package paths.
 Prevents Clashes in Large Projects
 Without packages, two classes named Logger (one for files,
one for network) would conflict.
 With packages:
05/22/2025 6

Types of Packages in Java

05/22/2025 7
Types of packages in Java
 Built-in Packages: These packages are part of the Java
API and provide pre-written classes and interfaces for
common tasks.
 Some commonly used built-in packages include:
 java.lang: Contains fundamental classes like String,
Math, and Thread. It is imported by default.
 java.io: Provides classes for input and output operations.
 java.util: Includes utility classes for data structures like
ArrayList and HashMap, and date/time operations.
 java.net: Contains classes for networking operations.
 java.sql: Provides classes for accessing and processing
data from databases.
 java.awt: Contains classes for creating graphical user 8
05/22/2025
Types of packages in Java cont..
User-defined Packages
These packages are created by developers to
organize their own classes and interfaces. They
help in modularizing code and improving
maintainability.
For example, a user might create a package
named com.example.myproject to store classes
related to their project.

05/22/2025 9
Creating and using Packages
 To create a package, you choose a name for the package
(consider naming conventions) and put a package statement
with that name at the top of every source file that contains
the types (classes, interfaces, enumerations, and annotation
types) that you want to include in the package.
 The package statement (for example, package graphics;)
must be the first line in the source file.
 There can be only one package statement in each source file,
and it applies to all types in the file.
 If you do not use a package statement, your type ends up in
an unnamed package. Generally speaking, an unnamed
package is only for small or temporary applications or when
you are just beginning the development process. Otherwise,
05/22/2025 10
Creating and using Packages cont..

Note:
- If you put multiple types in a single source file, only
one can be public, and it must have the same name
as the source file. For example, you can define
public class Circle in the file Circle.java, define
public interface Draggable in the file
Draggable.java, define public enum Day in the file
Day.java, and so forth.
- You can include non-public types in the same file as a
public type (this is strongly discouraged, unless the
non-public types are small and closely related to the
05/22/2025 11
public type), but only the public type will be
Creating and using Packages cont..

 With programmers worldwide writing classes and interfaces


using the Java programming language, it is likely that many
programmers will use the same name for different types.
 For example, a programmer might defines a Rectangle class
when there is already a Rectangle class in the java.awt
package. Still, the compiler allows both classes to have the
same name if they are in different packages.
 The “fully qualified name” of each Rectangle class includes
the package name. That is, the fully qualified name of the
Rectangle class in the graphics package is
graphics.Rectangle, and the fully qualified name of the
Rectangle class in the java.awt package is
java.awt.Rectangle.
05/22/2025 12
Naming conventions
Package names are written in all lower case to
avoid conflict with the names of classes or
interfaces.
Companies use their reversed Internet domain
name to begin their package names—for example,
com.example.mypackage for a package named
mypackage created by a programmer at
example.com.
Name collisions that occur within a single company
need to be handled by convention within that
company, perhaps by including the region or the
05/22/2025 13
project name after the company name (for example,
Naming conventions cont..
In some cases, the internet domain name may not be
a valid package name. This can occur if the domain
name contains a hyphen or other special character, if
the package name begins with a digit or other
character that is illegal to use as the beginning of a
Java name, or if the package name contains a
reserved Java keyword, such as "int". In this event,
the suggested convention is to add an underscore. For
example:

05/22/2025 14
Using package members
The types that comprise a package are known
as the package members.
To use a public package member from outside
its package, you must do one of the following:
a)Refer to the member by its fully qualified
name
b)Import the package member
c)Import the member's entire package
Each is appropriate for different situations, as
explained in the slides that follow.
05/22/2025 15
a) Referring to a package member by its Fully qualifi ed
name
 So far, most of the examples in this tutorial have referred to
types by their simple names, such as “MyClass”. You can use a
package member's simple name if the code you are writing is
in the same package as that member or if that member has
been imported.
 However, if you are trying to use a member from a different
package and that package has not been imported, you must
use the member's fully qualified name, which includes the
package name. Here is the fully qualified name for the
Rectangle class declared in the graphics package:
graphics.Rectangle
 You could use this qualified name to create an instance of
graphics.Rectangle,
05/22/2025 like: 16
a) Referring to a package member by its Fully qualifi ed
name

//save by A.java //save by B.java

package pack; package mypack;


public class A { class B {
public void msg() { public static void
main(String args[]) {
System.out.println("Hel pack.A obj = new
lo"); pack.A();
} obj.msg();
} }
05/22/2025 } 17
a) Referring to a package member by its Fully qualifi ed
name

 Qualified names are all right for infrequent


use. When a name is used repetitively,
however, typing the name repeatedly becomes
tedious and the code becomes difficult to
read. As an alternative, you can import the
member or its package and then use its
simple name.

05/22/2025 18
b) Importing a package member
To import a specific member into the current file, put an
import statement at the beginning of the file before any
type definitions but after the package statement, if there
is one.
Here's how you would import the Rectangle class from
the graphics package:
import graphics.Rectangle;
Now you can refer to the Rectangle class by its simple
name:
Rectangle myRect = new Rectangle();
This approach works well if you use just a few members
from the graphics package. But if you use many types
05/22/2025 19
b) Importing a package member

//save by B.java
//save by A.java
package mypack;
package pack;
public class A { import pack.A;
public void msg() { class B {
public static void
System.out.println("Hell main(String args[]) {
o"); A obj = new A();
} obj.msg();
} }
}
05/22/2025 20
c) Importing the entire package

To import all the types contained in a


particular package, use the import statement
with the asterisk (*) wildcard character.
import graphics.*;
Now you can refer to any class or interface in
the graphics package by its simple name eg:
Rectangle myRect = new Rectangle(); //
assuming the Rectangle class
// is in the graphics
05/22/2025 package: 21
c) Importing the entire package

//save by First.java //save by Second.java

package learnjava; package Java;


public class First{ import learnjava.*;
public void msg() class Second {
{ public static void main(String
args[]) {
System.out.println( First obj = new First();
"Hello"); obj.msg();
} }
}05/22/2025 } 22
c) Importing the entire package
Note that the asterisk in the import statement
can be used only to specify all the classes
within a package. It cannot be used to match a
subset of the classes in a package. For
example, the following does not match all the
classes in the graphics package that begin with
A (Instead, it generates a compiler error).
import graphics.A*; // does not work
With the import statement, you generally
import only a single package member or an
entire package.
05/22/2025 23
Apparent hierarchies of packages
 At first, packages appear to be hierarchical, but they are not.
 For example, the Java API includes a java.awt package, a
java.awt.color package, a java.awt.font package, and many
others that begin with java.awt.
 However, the java.awt.color package, the java.awt.font
package, and other java.awt.xxxx packages are not included
in the java.awt package. The prefix java.awt (the Java
Abstract Window Toolkit) is used for a number of related
packages to make the relationship evident, but not to show
inclusion.
 Importing java.awt.* imports all of the types in the java.awt
package, but it does not import java.awt.color,
java.awt.font, or any other java.awt.xxxx packages. If you
plan to use the classes and other types in java.awt.color as24
05/22/2025
Tutorial Question
There are three alternatives that you would
use to access a public package member
from outside its package:
i. Referring to the member by its fully
qualified name
ii. Importing the package member
iii.Importing the member's entire package
–What advantage does the first approach
(Referring to the member by its fully qualified
name) have over the other two approaches?
05/22/2025 25
END OF LECTURE THIRTEEN

05/22/2025 26
NEXT ON LECTURE
FOURTEEN

JAVA INTERFACE

05/22/2025 27
THANK YOU!

05/22/2025 28

You might also like