0% found this document useful (0 votes)
37 views13 pages

Programming With Java For Language Developers Csa0711

Uploaded by

Ruchita Maaran
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)
37 views13 pages

Programming With Java For Language Developers Csa0711

Uploaded by

Ruchita Maaran
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/ 13

PROGRAMMING WITH JAVA

FOR LANGUAGE
DEVELOPERS
[CSA0711]
PACKAGES & ACCESS PROTECTION

RUCHITA MAARAN 252310022 II BCA - 'A' SLOT - A


Java Packages
Package in Java is a mechanism to encapsulate a
group of classes, sub packages and interfaces.

The package keyword is used to create a package in


java.

They allow you to group related classes, interfaces,


and sub-packages together, making your application
more modular, maintainable, and secure.

1. A java package is a group of similar types of classes, interfaces and sub-packages.


2. Package in java can be categorized in two form, built-in package and user-defined package.
3. There are many built-in packages such as java, lang, awt, , net, io, util, sql etc.
4. Every class is part of some package.
5. We can access public classes in another (named) package using: package-name.class-name
Subpackages

• Package inside the package is called the


subpackage.
• It should be created to categorize the
package further.
• These are not imported by default, they have
to imported explicitly.
• Also, members of a subpackage have no
access privileges, i.e., they are considered as
different package for protected and default
access specifiers.
Advantages of Java Packages
1 Categorization 2 Access Protection
Java packages help categorize your classes and Packages provide access control, allowing you
interfaces, making it easier to maintain and to control the visibility and accessibility of
navigate your codebase. your class members.

3 Naming Collision Prevention 4 Encapsulation


Packages help prevent naming conflicts by Packages can be considered a form of data
providing a unique namespace for your classes encapsulation, hiding the implementation
and interfaces. details of your classes from the outside world.
How to access package from another package?

Import the Package 1


Use the import package.*; syntax to
import all classes from a package.
2 Import the Class
Use the import package.ClassName;
syntax to import a specific class from a
Use the Fully Qualified Name 3 package.
Use the fully qualified name, such as
package.ClassName, to access a class
from another package.
Accessing Packages
Import the Package Import the Class Use the Fully Qualified
Name
Built-in Java Packages
Pre-defined packages and these packages contain large numbers of classes and interfaces that we used in java are
known as Built-in packages.

java.lang java.io java.sql java.awt


Contains fundamental Provides classes for Contains classes for Includes classes for
language support input/output operations, accessing and creating user interfaces
classes, such as such as file handling processing data stored and handling graphics
String, Math, and and networking. in a database. and images.
System.

User Defined Packages


These are the packages that are defined by the user.
Access Protection in
Java

1. Access Protection is a mechanism that


controls the visibility and accessibility of
class members(fields, methods and
constructors) from other classes.
2. In java, the access modifiers define the
accessibility of the class and its members.
3. In java, the package is a container of classes,
sub-classes, interfaces, and sub-packages.
4. The class acts as a container of data and
methods.
5. So, the access modifier decides the
accessibility of class members across the
different packages.
Access Modifiers in Java Packages

Public Protected Default Private

The public access The protected The default access The private access
modifier allows access access modifier allows modifier (no keyword) modifier restricts
from any class, access from any class allows access from access to only the
regardless of the in the same package any class in the same class that contains it.
package. and any subclass in a package, but not from
different package. classes in other
packages.
Organizing Packages
Logical Grouping Naming Conventions
Group related classes, interfaces, and sub- Follow the reverse domain name convention (e.g.,
packages together to maintain a logical and com.company.project) to ensure unique and
organized structure. meaningful package names.

Subpackages Encapsulation
Use subpackages to further categorize and Leverage packages to encapsulate implementation
organize your code, but be mindful of access details and provide a clear, well-defined API for
control and dependencies. your classes and interfaces.
THANK YOU

You might also like