0% found this document useful (0 votes)
16 views

Packages

Uploaded by

arunanirmal2004
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)
16 views

Packages

Uploaded by

arunanirmal2004
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/ 34

Packages

Presented by
Ms.G.Sakthi Priya, AP/CSE
Introduction
• Unique name had to be used for each class to avoid name collisions.
• Name you choose for a class will be reasonably unique and not
collide with other class names
For example, a package allows you to create a class named List,
which you can store in your own package without concern that it will
collide with some other class named List stored elsewhere
• Java provides a mechanism for partitioning the class name space into
more manageable chunks. This mechanism is the package
• The package is both a naming and a visibility control mechanism.
• You can define classes inside a package that are not accessible by
code outside that package
• You can also define class members that are exposed only to other
members of the same define class members that are exposed only to
other members of the same package
• This allows your classes to have intimate knowledge of each other,
but not expose that knowledge to the rest of the world
Package
• Packages are containers for classes. They are used to keep the
class name space compartmentalized.
• A Java package is a mechanism for organizing similar type of
Java classes, sub packages and interfaces into namespaces.
• Each package in Java has its unique name and it helps
organize the classes into a folder structure and make it easy
to locate and use them.
• Packages are stored in a hierarchical manner and are explicitly
imported into new class definitions.
Which package is always imported by default in Java?

java.lang package is imported by default, no need to explicitly


import it.

Classes in the java.lang package do not need to be imported.


Types of Packages
Built-in Packages
• java.lang: Automatically imported.
• java.io: input / output operations.
• java.util: Linked List, Dictionary and support ; for Date / Time operations.
• java.applet: classes for creating Applets.
• java.awt: classes for implementing the components for graphical user
interfaces (like button , ;menus etc).
• java.net: classes for supporting networking operations

• These packages consist of a large number of classes which are a part of


Java API
Defining a package
• The package statement defines a name space in which classes are
stored. If you omit the package statement, the class names are put
into the default package, which has no name and inadequate for real
applications
• More than one file can include the same package statement.
User defined package
• A user-defined package is a custom package created by the programmer
to group related classes, interfaces, and sub-packages.
• This helps organize code into a structured format, making it more
manageable, modular, and reusable.
• User-defined packages also help in avoiding naming conflicts by providing
unique namespaces for classes and interfaces.

• General form of the package statement:


package name_of_package;
the .class files for any classes you declare to
Example: be part of mypackage must be stored in a directory
package mypackage;
called mypackage.
Sub package
• Hierarchy of packages is called as sub packages.
• To create a hierarchy of a package, 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:
• package pkg1[.pkg2[.pkg3]];
Example: package a.b.c;
• You cannot rename a package without renaming the directory in
which the classes are stored
Example

1. Defining the package


package pack;

2. Create Classes within the Package:


package pack;
class Hello{
public static void main(String args[]){
System.out.println("Hello pacakges");
}
}
Example
3. Compile the Package: Navigate to the directory
where the package is located and
compile the Java file using
Javac –d . Filename.java

javac is used to compile .java source files into


.class bytecode files.
-d specifies the destination directory for the
compiled .class files.
. (dot) specifies the current directory as the
root directory for the package structure.
Sub packages
package pack.subpack;
class Hello{
public static void main(String args[]){
System.out.println("Hello pacakges");
}
}
Practice
• Create a public class called Bank with public parameterized
constructor whose members are name and bal and public method
show() to display the name and bal.

• Create a class AccountBalance to create an object for bank and


display the name and balance
package mypack;
class Balance{
String name;
double bal;
Balance(String n, double b){
name = n;
bal = b;
}
void show(){
System.out.println(name + " Rs "+ bal);

}
}
class AccountBalance{
public static void main(String[] args){
Balance current = new Balance("Tom",12345);
current.show();
}
Importing Packages
• Import statement allows to import the entire package or certain
classes/interface
Example:
• import java.util.Date;
• import java.io.*;
Example with the import Example without the import statement
statement class MyDate extends java.util.Date {
import java.util.*;
class MyDate extends }
Date {
import mypack.Balance;
Example: class AccountBalance{
public static void main(String[] args){
Balance current = new Balance("Tom",12345);
package mypack; current.show();
public class Balance{ }
String name; }
double bal;
public Balance(String n, double b){
name = n;
bal = b;
}
public void show(){
System.out.println(name + " Rs "+ bal);

} Execution: java AccountBalance


} Output:Tom Rs 12345.0
Practice
• Creating our first package: firstpack
File name – One.java with System.out.println("Hello I am class One");

• Creating our second package: secondpack


File name – Two.java with System.out.println("Hello I am class Two");

• Making use of both the created packages:


File name – Testing.java
package firstpack;
package secondpack;
public class one{
public class two{
public void show(){
public void show(){
System.out.println("Hello");
System.out.println("Hai");
}
}
}
}
import firstpack.one;
import secondpack.two;
class Testing{
public static void main(String[] args){
one o = new one();
two t=new two();
o.show();
t.show();
}
}
Class poll
package distanceconverter; import ___________________;
class converter class one
{ public double KmtoM(double k) {
{ public static void main( String[] args)
double m= k*1000; {
return m; ________ obj= new _______;
} double m = obj.KmtoM(12.5);
public double MtoKM(double m) System.out.println(m);
{ double m2 = obj.MtoKM(1252);
double k= m/1000; System.out.println(m2);
return k; }
} }
}
Purpose of package:

• Java package is used to categorize the classes and interfaces so that


they can be easily maintained- Better Organization and locating the
classes easier
• It also helps resolve naming conflicts when different packages have
classes with the same names.
• Java package provides access protection: Packages access level also
allows to protect data from being used by the non-authorized
classes.
• Reusability.
Packages and member access
• Java addresses four categories of visibility for class members:
• Subclasses in the same package
• Non-subclasses in the same package
• Subclasses in different packages
• Classes that are neither in the same package nor subclasses
Packages and member access
• Anything declared private cannot be seen outside of its class.
• Anything declared public can be accessed from different classes and
different packages.
• When a member does not have an explicit access specification, it is
visible to subclasses as well as to other classes in the same package.
This is the default access.
• If you want to allow an element to be seen outside your current
package (different package), but only to classes that subclass your
class directly, then declare that element protected.
• Private – same class
Hint to remember

• Default – same class - same package


subclass / other class - same package

• Protected - same class – same package


subclass / other class – same package
only to sub class -different package

• Public – any class


any package
Private Access
package p1;
package p1; class privateDemo{
public class Protect{ public static void main(String[] args){
int def=3; Protect obj = new Protect();
private int pri =4; System.out.println(obj.pri);
protected int pro=5; }
public int pub=6; }
}
Explanation
• The "protect" class contains a private variable named "pri" with a
value of 4.
• However, when an attempt is made to access this variable in another
class called "private Demo," an error occurs.
Default Access
Default access - visible to other class as well as to subclasses in the same package.
package p1;
public class Protect{ package p1;
int def=3; class defaultDemo extends Protect{
private int pri =4; public static void main(String[] args){
protected int pro=5; Protect obj = new Protect();
public int pub=6; System.out.println(obj.def);
} }
package p1; }
class defaultDemo
public static void main(String[] args){
Protect obj = new Protect();
// produce output when used in package p1
System.out.println(obj.def);
}
}
Explanation
• Inside the "p1" package, there exists a class named "Protect" with a
default variable named "def," which is initialized with a value of 3.

• The variable "def" is accessible when accessed through an object


within a class called "defaultDemo," which resides within the same
"p1" package. When this variable is accessed, it will output the value 3.

• Furthermore, the "def" variable is inherited in the "defaultDemo" class,


which is still within the "p1" package. This inheritance is valid, and it
will result in the output of 3 when the variable is accessed.
Default Access
package p2;
import p1.Protect;
class defaultDemo extends Protect
public static void main(String[] args){
package p1;
public class Protect { Protect obj = new Protect();
int def=3; //won't work on other package
private int pri =4; System.out.println(obj.def);
protected int pro=5; }
public int pub=6; }
}

Not accessible in
different package
Explanation
• Inside the "p1" package, there exists a class named "Protect" with a
default variable named "def," which is initialized with a value of 3.

• The "def" variable is inherited in the "defaultDemo" class, which is in


different package “p2”. This inheritance is invalid, and it will result in
error
Protected Access - Element to be seen outside your
current package (different package), but only to classes
that subclass your class directly
package p2;
import p1.Protect;
package p1; class protectedDemo extends Protect{
public class Protect{ public static void main(String[] args){
int def=3; protectedDemo obj = new protectedDemo();
private int pri =4; System.out.println(obj.pro); //outputs 5
protected int pro=5;
public int pub=6; }
} }
package p2;
import p1.Protect;
class protectedDemo extends Protect{
public static void main(String[] args){
//Following code is error
Protect o = new Protect();
System.out.println(o.pro);
}
}
Explanation
• Inside the "p1" package, there exists a class named "Protect" with a
protected variable named “pro," which is initialized with a value of 5.

• The “pro" variable is inherited in the “protectedDemo" class, which is


in different package “p2” and accessed using subclass object. This
inheritance is valid, and it will result in 5

• The “pro" variable is inherited in the “protectedDemo" class, which is


in different package “p2” and accessed using superclass object. This
inheritance is invalid, and it will result in occur
Public access
package p2;
package p1;
import p1.Protect;
public class Protect{
class publicDemo extends Protect{
int def=3;
public static void main(String[] args){
private int pri =4;
Protect obj = new Protect();
protected int pro=5;
System.out.println(obj.pub);
public int pub=6;
}
}
}
Explanation
• Inside the "p1" package, there exists a class named "Protect" with a
public variable named “pub," which is initialized with a value of 6.
• The “pro" variable is inherited in the “protectedDemo" class, which is
in different package “p2” and accessed using superclass object. This
inheritance is valid, and it will result in 6

You might also like