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

Understanding Java APIs and Packages

The document provides an overview of Java APIs and packages, explaining their purpose in organizing classes and enhancing modularity, maintainability, and code reuse. It covers key features of packages, how to create and import them, and includes practical exercises for creating custom packages. Additionally, it discusses the importance of import statements in Java programming.

Uploaded by

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

Understanding Java APIs and Packages

The document provides an overview of Java APIs and packages, explaining their purpose in organizing classes and enhancing modularity, maintainability, and code reuse. It covers key features of packages, how to create and import them, and includes practical exercises for creating custom packages. Additionally, it discusses the importance of import statements in Java programming.

Uploaded by

Verma Astha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Learn Like a GEM

Java API’s and Packages


Do Now

Write the answers for the following in your notebooks.


1. How does grouping make things easier in the real-world?
2. List real-life examples where items are grouped.
Learning Outcomes To Know

At the end of this lesson, you


should be able to how to organize methods inside
explain the purpose of Java APIs and classes in Java
packages, create and organize custom
packages, and demonstrate how what are Library classes in Java
packages enhance modularity,
maintainability, and code reuse in Java how built-in library classes are
applications.
organized in Java
I Do

What is an API?

The Java Application Programming Interface API is the complete set of


packages provided by Java to perform tasks like data manipulation, networking, file
I/O, and more.
The Java API is made up of hundreds of packages, each serving a specific
purpose.
I Do
Definition of Packages

A package in Java is a namespace that organizes classes and interfaces. It helps


manage the code by grouping related classes together, avoiding naming conflicts,
and enabling better modularity and reusability.
It is analogous to a folder in Windows Explorer that stores related files.
Examples of in-built core java packages:
[Link], [Link], [Link], [Link], [Link] etc.,
I Do
Key Features of Packages:
Organization: Helps group similar classes and interfaces. Makes large projects
easier to navigate and manage.

Access Control: Packages help enforce encapsulation. Enables controlled access


using access modifiers (e.g., public, protected, private). Classes with no access
modifiers (default access) are only visible within the same package.

Namespace Management: Avoids naming conflicts by providing unique


namespaces. There are can be two classes that have the same name in two
different packages.

Reusability: Encourages, modular design, avoids duplication and reusability of


code across projects.
I Do

Importing in-built packages


import [Link];
java → the base package for Java
util → the utility subpackage (contains useful classes like Scanner, ArrayList, etc.)
Scanner → the specific class you're importing
For importing all the classes in util
import [Link].*;
I Do

Creating a Package

Source code files that contain classes or interfaces are typically organized into
packages. To include a source file in a package, use the package keyword
followed by the package name at the very top of the Java file. When you compile
the file, it will be placed in a directory structure that matches the package name.
Example:
package MyPackage;
public class MyClass {
public void displayMessage() {
[Link]("Hello from MyPackage!");
} }
After compiling, the directory structure will look like MyPackage/[Link] (byte code version)
I Do

Importing User-Defined Packages: To use a package in another program:


Import the package using the import keyword.

Example:
import [Link];
public class Main {
public static void main() {
MyClass obj = new MyClass();
[Link]();
}
}
We Do

Create a package named Mypack in the current project


or
Create a new project in Bluej. Name it Myfiles. Inside the new project Myfiles, create a package
named Mypack.
Inside the package, create a class MyMath with the following methods:
int square(int n) → returns the square of a number.
int cube(int n) → returns the cube of a number.

Create another class named TestPackage (outside the Mypack package but inside the current
project or the Myfiles project) that imports [Link], Creates an object of MyMath, Accepts
an integer from the user and Displays its square and cube.
We Do Solution

import [Link];
package mypack;
public class MyMath { public class TestPackage {
public int square(int n) { public static void main() {
return n * n; MyMath obj = new MyMath();
} [Link]("Square of 5 = " + [Link](5));
public int cube(int n) { [Link]("Cube of 3 = " + [Link](3));
return n * n * n; }
} }
}
You Do
Create a package Myutil in the current folder. Organize the following classes in it.

Date
MyArray
TwoD_Array
Convert

The codes for the above classes were discussed and completed in grade 11. Go to
Windows Explorer and move the files to Myutil package. Myutil package will appear
as a folder in Windows Explorer.

If main() programs already exist for the above classes, then move them to the current
folder, otherwise write a class called test_class in the current folder, to test one or
more of the above classes.
Stretch It

Create a package shapes in the current folder.


Create an Interface Geofigure that has the prototype of the methods
area() and perimeter()
Create class Circle that implements Geofigure with the
methods area() and perimeter().
Create class Rectangle that implements Geofigure with the
methods area() and perimeter().

Write a class called Geomain in the current folder that has a menu-
driven program to use the Circle class and the Rectangle class.
Affirmative Checking
Is the import statement essential? What will be the consequence of not including the import
statement in a program?
Observe the code given below:
//import Mypack.*;
//import [Link];
class testMyMath {
public static void main() {
[Link] ob=new [Link]([Link]);
[Link]("Enter a number:");
int k=[Link]();
[Link]([Link](5));
[Link]([Link](5));
}
}
In the above program as import statements are not included, the Scanner class must be prefixed with
the names of the packages, [Link] and the MyMath class must be prefixed with the package name
Mypack. Prefixing the package name every time a class is used will be very tedious for a programmer.
Hence it is better to include the import statement.
Exit Ticket
Q1. In Java, what is the purpose of a package?
a) To define variables b) To group related classes
c) To create loops d) To execute the program

Q2. Which of the following is the correct way to declare a package?


a) package = mypack; b) mypack package;
c) package mypack; d) pack mypack;

Q3. Which one of the following packages is the backbone for all Java programs?
a) awt b) util c) lang d) io

Q4. Where must the package declaration (package mypack;) be placed in a Java file?
a) At the end of the class b) Before public class statement (first line)
c) Anywhere in the program d) After import statement

You might also like