SlideShare a Scribd company logo
Packages
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
2
Packages and Interfaces
 Two of Java’s most innovative features:
 packages and interfaces
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
3
Packages
 In general, a unique name had to be used for each class to
avoid name collisions
 After a while, without some way to manage the name
space, you could run out of convenient, descriptive names
for individual classes.
 Java provides a mechanism for partitioning the class name
space into more manageable chunks. - Package
 A package in Java is used to group related classes.
 Think of it as a folder in a file directory. We use packages
to avoid name conflicts, and to write a better
maintainable code.
 The package is both a naming and a visibility control
mechanism. --- HOW??
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
4
Uses
 Packages are used for:
 A Preventing naming conflicts.
 For example there can be two classes with name
Employee in two packages,
 college.staff.cse.Employee and
college.staff.ee.Employee
 Providing controlled access:
 protected and default have package level access
control. A protected member is accessible by classes in
the same package and its subclasses. A default member
(without any access specifier) is accessible by classes
in the same package only.
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
5
Defining a Package
 To create a package simply include a package command
as the first statement in a Java source file.
 Any classes declared within that file will belong to the
specified 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.
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
6
Defining a Package
C:UsersYour Name>javac -d . Package_Ex.java
C:UsersYour Name>java mypack.Package_Ex
To execute, class name must be qualified with its package name.
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Defining a Package
 This forces the compiler to create the "mypack" package.
 The -d keyword specifies the destination for where to
save the class file.
 You can use any directory name, like c:/user (windows),
or, if you want to keep the package within the same
directory, you can use the dot sign ".", like in the example
above.
 Note: The package name should be written in lower case
to avoid conflict with class names.
7Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Access Protection
 Classes and packages are both means of encapsulating and
containing the name space and scope of variables and
methods.
 Classes act as containers for data and code.
 The class is Java’s smallest unit of abstraction.
 Packages act as containers for classes and other
subordinate packages.
 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
8Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Access Protection
 Public - Anything declared public can be accessed from
anywhere.
 Private - Anything declared private cannot be seen outside
of its class.
 Package - 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.
 Protected - If you want to allow an element to be seen
outside your current package, but only to classes that
subclass your class directly
9Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Access Protection
10Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Access within Same Package
Demo
11Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Access from other Package
 Importing Packages
 Java includes the import statement to bring certain
classes, or entire packages, into visibility.
 Once imported, a class can be referred to directly, using
only its name.
 In a Java source file, import statements occur
immediately following the package statement (if it exists)
and before any class definitions.
 This is the general form of the import statement:
12Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Access from other Package
 pkg1 is the name of a top-level package, and pkg2 is the
name of a subordinate package inside the outer package
separated by a dot (.).
 There is no practical limit on the depth of a package
hierarchy.
 Finally, you specify either an explicit classname or a star
(*), which indicates that the Java compiler should import
the entire package.
 This code fragment shows both forms in use:
13Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Accessing Classes in a Package
 Fully Qualified class name:
Example:
java.util.Scanner s=new java.util.Scanner(System.in);
 import packagename.classname;
Example: import java.util. Scanner;
or
import packagename.*;
Example: import java.util.*;
Import statement must appear at the top of the file, before
any class declaration.
14Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Info bits!!!!
 The star form may increase compilation time—especially if you
import several large packages.
 For this reason it is a good idea to explicitly name the classes
that you want to use rather than importing whole packages.
 However, the star form has absolutely no effect on the run-time
performance or size of your classes.
 All of the standard Java classes included with Java are stored in
a package called java.
 The basic language functions are stored in a package inside of
the java package called java.lang.
 Normally, you have to import every package or class that you
want to use, but since Java is useless without much of the
functionality in java.lang, it is implicitly imported by the
compiler for all programs.
15Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Package Types
 Packages are divided into two categories:
 Built-in Packages (packages from the Java API)
 User-defined Packages (create your own packages)
 Built-in Packages
 The Java API is a library of prewritten classes, that are free to use,
included in the Java Development Environment.
 The complete list can be found at Oracles website:
https://docs.oracle.com/javase/8/docs/api/.
 User-defined Packages
 These are the packages that are defined by the user.
 To create your own package, you need to understand that Java
uses a file system directory to store them. Just like folders on your
computer
16Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Example Built-in Packages
 1) java.lang: Contains language support classes(e.g classed
which defines primitive data types, math operations). This
package is automatically imported.
 2) java.io: Contains classed for supporting input / output
operations.
 3) java.util: Contains utility classes which implement data
structures like Linked List, Dictionary and support ; for Date /
Time operations.
 4) java.applet: Contains classes for creating Applets.
 5) java.awt: Contain classes for implementing the components
for graphical user interfaces (like button , ;menus etc).
 6) java.net: Contain classes for supporting networking
operations.
17Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Example1-Package
package p1;
public class ClassA
{
public void displayA( )
{
System.out.println(“Class A”);
}
}
import p1.*;
Class testclass
{
public static void main(String str[])
{
ClassA obA=new ClassA();
obA.displayA();
}
}
18Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Example2-Package
package p2;
public class ClassB
{
protected int m =10;
public void displayB()
{
System.out.println(“Class B”);
System.out.println(“m= “+m);
}
}
import p1.*;
import p2.*;
class PackageTest2
{
public static void main(String str[])
{
ClassA obA=new ClassA();
Classb obB=new ClassB();
obA.displayA();
obB.displayB();
}
}
19Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Example 3- Package
import p2.ClassB;
class ClassC extends ClassB
{
int n=20;
void displayC()
{
System.out.println(“Class C”);
System.out.println(“m= “+m);
System.out.println(“n= “+n);
}
}
class PackageTest3
{
public static void main(String args[])
{
ClassC obC = new ClassC();
obC.displayB();
obC.displayC();
}
}
20Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
The End…
21Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam

More Related Content

What's hot

Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
HrithikShinde
 
Files in java
Files in javaFiles in java
Java package
Java packageJava package
Java package
CS_GDRCST
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
Prognoz Technologies Pvt. Ltd.
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Single inheritance
Single inheritanceSingle inheritance
Single inheritance
Äkshäý M S
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
Abid Kohistani
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
 
Course 102: Lecture 24: Archiving and Compression of Files
Course 102: Lecture 24: Archiving and Compression of Files Course 102: Lecture 24: Archiving and Compression of Files
Course 102: Lecture 24: Archiving and Compression of Files
Ahmed El-Arabawy
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streamsShahjahan Samoon
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
Hitesh Kumar
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
Khaled Adnan
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
Ronak Chhajed
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
cprogrammings
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
Arslan Waseem
 

What's hot (20)

Packages in java
Packages in javaPackages in java
Packages in java
 
encapsulation
encapsulationencapsulation
encapsulation
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
 
Files in java
Files in javaFiles in java
Files in java
 
Java package
Java packageJava package
Java package
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 
Java packages
Java packagesJava packages
Java packages
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Single inheritance
Single inheritanceSingle inheritance
Single inheritance
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Course 102: Lecture 24: Archiving and Compression of Files
Course 102: Lecture 24: Archiving and Compression of Files Course 102: Lecture 24: Archiving and Compression of Files
Course 102: Lecture 24: Archiving and Compression of Files
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 

Similar to Java - Packages Concepts

Java packages oop
Java packages oopJava packages oop
Java packages oop
Kawsar Hamid Sumon
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packages
TharuniDiddekunta
 
Packages
PackagesPackages
Packages
Monika Mishra
 
Packages in java
Packages in javaPackages in java
Packages in java
SahithiReddyEtikala
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
SanthiNivas
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
pankaj chelak
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
Vishnu Suresh
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
talha ijaz
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
Kuntal Bhowmick
 
Packages in java
Packages in javaPackages in java
Packages in java
Elizabeth alexander
 
Package in Java
Package in JavaPackage in Java
Package in Java
lalithambiga kamaraj
 
Packages in java
Packages in javaPackages in java
Packages in java
jamunaashok
 
Packages in java
Packages in javaPackages in java
Packages in java
Jancypriya M
 
Unit4 java
Unit4 javaUnit4 java
Unit4 javamrecedu
 
Java packages
Java packagesJava packages
Java packages
Jeffrey Quevedo
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
happycocoman
 
Package.pptx
Package.pptxPackage.pptx
Package.pptx
VeenaNaik23
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
Kuntal Bhowmick
 
150950107056 2150704
150950107056 2150704150950107056 2150704
150950107056 2150704
Prashant Mokani
 

Similar to Java - Packages Concepts (20)

Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packages
 
Packages
PackagesPackages
Packages
 
Packages in java
Packages in javaPackages in java
Packages in java
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
Java packages
Java packagesJava packages
Java packages
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
Package.pptx
Package.pptxPackage.pptx
Package.pptx
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
150950107056 2150704
150950107056 2150704150950107056 2150704
150950107056 2150704
 

More from Victer Paul

OOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Sequence and Communication Diagrams - LabOOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Sequence and Communication Diagrams - Lab
Victer Paul
 
OOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - LabOOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - Lab
Victer Paul
 
OOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation ConceptsOOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation Concepts
Victer Paul
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings Concepts
Victer Paul
 
Java - OOPS and Java Basics
Java - OOPS and Java BasicsJava - OOPS and Java Basics
Java - OOPS and Java Basics
Victer Paul
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
Victer Paul
 
Java - Class Structure
Java - Class StructureJava - Class Structure
Java - Class Structure
Victer Paul
 
Java - Object Oriented Programming Concepts
Java - Object Oriented Programming ConceptsJava - Object Oriented Programming Concepts
Java - Object Oriented Programming Concepts
Victer Paul
 
Java - Basic Concepts
Java - Basic ConceptsJava - Basic Concepts
Java - Basic Concepts
Victer Paul
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
Victer Paul
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
Victer Paul
 
Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays Concepts
Victer Paul
 
Java applet programming concepts
Java  applet programming conceptsJava  applet programming concepts
Java applet programming concepts
Victer Paul
 

More from Victer Paul (13)

OOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Sequence and Communication Diagrams - LabOOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Sequence and Communication Diagrams - Lab
 
OOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - LabOOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - Lab
 
OOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation ConceptsOOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation Concepts
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings Concepts
 
Java - OOPS and Java Basics
Java - OOPS and Java BasicsJava - OOPS and Java Basics
Java - OOPS and Java Basics
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
 
Java - Class Structure
Java - Class StructureJava - Class Structure
Java - Class Structure
 
Java - Object Oriented Programming Concepts
Java - Object Oriented Programming ConceptsJava - Object Oriented Programming Concepts
Java - Object Oriented Programming Concepts
 
Java - Basic Concepts
Java - Basic ConceptsJava - Basic Concepts
Java - Basic Concepts
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
 
Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays Concepts
 
Java applet programming concepts
Java  applet programming conceptsJava  applet programming concepts
Java applet programming concepts
 

Recently uploaded

UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 

Recently uploaded (20)

UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 

Java - Packages Concepts

  • 1. Packages Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 2. 2 Packages and Interfaces  Two of Java’s most innovative features:  packages and interfaces Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 3. 3 Packages  In general, a unique name had to be used for each class to avoid name collisions  After a while, without some way to manage the name space, you could run out of convenient, descriptive names for individual classes.  Java provides a mechanism for partitioning the class name space into more manageable chunks. - Package  A package in Java is used to group related classes.  Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code.  The package is both a naming and a visibility control mechanism. --- HOW?? Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 4. 4 Uses  Packages are used for:  A Preventing naming conflicts.  For example there can be two classes with name Employee in two packages,  college.staff.cse.Employee and college.staff.ee.Employee  Providing controlled access:  protected and default have package level access control. A protected member is accessible by classes in the same package and its subclasses. A default member (without any access specifier) is accessible by classes in the same package only. Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 5. 5 Defining a Package  To create a package simply include a package command as the first statement in a Java source file.  Any classes declared within that file will belong to the specified 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. Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 6. 6 Defining a Package C:UsersYour Name>javac -d . Package_Ex.java C:UsersYour Name>java mypack.Package_Ex To execute, class name must be qualified with its package name. Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 7. Defining a Package  This forces the compiler to create the "mypack" package.  The -d keyword specifies the destination for where to save the class file.  You can use any directory name, like c:/user (windows), or, if you want to keep the package within the same directory, you can use the dot sign ".", like in the example above.  Note: The package name should be written in lower case to avoid conflict with class names. 7Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 8. Access Protection  Classes and packages are both means of encapsulating and containing the name space and scope of variables and methods.  Classes act as containers for data and code.  The class is Java’s smallest unit of abstraction.  Packages act as containers for classes and other subordinate packages.  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 8Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 9. Access Protection  Public - Anything declared public can be accessed from anywhere.  Private - Anything declared private cannot be seen outside of its class.  Package - 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.  Protected - If you want to allow an element to be seen outside your current package, but only to classes that subclass your class directly 9Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 10. Access Protection 10Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 11. Access within Same Package Demo 11Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 12. Access from other Package  Importing Packages  Java includes the import statement to bring certain classes, or entire packages, into visibility.  Once imported, a class can be referred to directly, using only its name.  In a Java source file, import statements occur immediately following the package statement (if it exists) and before any class definitions.  This is the general form of the import statement: 12Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 13. Access from other Package  pkg1 is the name of a top-level package, and pkg2 is the name of a subordinate package inside the outer package separated by a dot (.).  There is no practical limit on the depth of a package hierarchy.  Finally, you specify either an explicit classname or a star (*), which indicates that the Java compiler should import the entire package.  This code fragment shows both forms in use: 13Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 14. Accessing Classes in a Package  Fully Qualified class name: Example: java.util.Scanner s=new java.util.Scanner(System.in);  import packagename.classname; Example: import java.util. Scanner; or import packagename.*; Example: import java.util.*; Import statement must appear at the top of the file, before any class declaration. 14Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 15. Info bits!!!!  The star form may increase compilation time—especially if you import several large packages.  For this reason it is a good idea to explicitly name the classes that you want to use rather than importing whole packages.  However, the star form has absolutely no effect on the run-time performance or size of your classes.  All of the standard Java classes included with Java are stored in a package called java.  The basic language functions are stored in a package inside of the java package called java.lang.  Normally, you have to import every package or class that you want to use, but since Java is useless without much of the functionality in java.lang, it is implicitly imported by the compiler for all programs. 15Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 16. Package Types  Packages are divided into two categories:  Built-in Packages (packages from the Java API)  User-defined Packages (create your own packages)  Built-in Packages  The Java API is a library of prewritten classes, that are free to use, included in the Java Development Environment.  The complete list can be found at Oracles website: https://docs.oracle.com/javase/8/docs/api/.  User-defined Packages  These are the packages that are defined by the user.  To create your own package, you need to understand that Java uses a file system directory to store them. Just like folders on your computer 16Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 17. Example Built-in Packages  1) java.lang: Contains language support classes(e.g classed which defines primitive data types, math operations). This package is automatically imported.  2) java.io: Contains classed for supporting input / output operations.  3) java.util: Contains utility classes which implement data structures like Linked List, Dictionary and support ; for Date / Time operations.  4) java.applet: Contains classes for creating Applets.  5) java.awt: Contain classes for implementing the components for graphical user interfaces (like button , ;menus etc).  6) java.net: Contain classes for supporting networking operations. 17Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 18. Example1-Package package p1; public class ClassA { public void displayA( ) { System.out.println(“Class A”); } } import p1.*; Class testclass { public static void main(String str[]) { ClassA obA=new ClassA(); obA.displayA(); } } 18Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 19. Example2-Package package p2; public class ClassB { protected int m =10; public void displayB() { System.out.println(“Class B”); System.out.println(“m= “+m); } } import p1.*; import p2.*; class PackageTest2 { public static void main(String str[]) { ClassA obA=new ClassA(); Classb obB=new ClassB(); obA.displayA(); obB.displayB(); } } 19Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 20. Example 3- Package import p2.ClassB; class ClassC extends ClassB { int n=20; void displayC() { System.out.println(“Class C”); System.out.println(“m= “+m); System.out.println(“n= “+n); } } class PackageTest3 { public static void main(String args[]) { ClassC obC = new ClassC(); obC.displayB(); obC.displayC(); } } 20Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 21. The End… 21Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam