SlideShare uma empresa Scribd logo
1 de 2
Baixar para ler offline
3/30/2019 Packages - access protection, importing packages: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/packages-access-protection-importing-packages?module_item_id=21012860 1/2
Packages - access protec on, impor ng
packages
Package
Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.
Packages are used for:
Java package is used to categorize the classes and interfaces so that they can be easily maintained.
Java package provides access protection.
Java package removes naming collision.
All related classes are put into packages. After that we can import a class from existing packages and use it in our
program. A packages is container of group of related classes where some of the classes are accessible are exposed
and others are kept for internal purpose.
We can reuse existing classes from the packages as many time as we need it in our program.
Adding a class to a Package : We can add more classes to an created package by using package name at the top of
the program and saving it in the package directory.
Subpackages: Packages that are inside another package are the subpackages. 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.
Types of packages:
Built-in Packages:
These packages consist of a large number of classes which are a part of Java API.Some of the commonly used built-
in packages are:
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.
User-defined packages:
These are the packages that are defined by the user. First we create a directory myPackage (name should be same as
the name of the package). Then create the MyClass inside the directory with the first statement being the package
names.
The package keyword is used to create a package in java.
//save by A.java in 'pack' directory
package pack;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}
There are three ways to access the package from outside the package.
3/30/2019 Packages - access protection, importing packages: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/packages-access-protection-importing-packages?module_item_id=21012860 2/2
1. import package.*;
2. import package.classname;
3. fully qualified name.
Using packagename.*
If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages.
import pack.*;
class Demo
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
Using packagename.classname
If you import package.classname then only declared class of this package will be accessible.
import pack.A;
class Demo
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
Using fully qualified name
If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to
import. But you need to use fully qualified name every time when you are accessing the class or interface.
It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date
class.
class Demo
{
public static void main(String args[])
{
A obj = new pack.A();
obj.msg();
}
}
Output:
Hello

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Java RMI
Java RMIJava RMI
Java RMI
 
Inheritance in c++theory
Inheritance in c++theoryInheritance in c++theory
Inheritance in c++theory
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Abstraction java
Abstraction javaAbstraction java
Abstraction java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Java program structure
Java program structure Java program structure
Java program structure
 
What is Constructors and Destructors in C++ (Explained with Example along wi...
What is Constructors and Destructors in  C++ (Explained with Example along wi...What is Constructors and Destructors in  C++ (Explained with Example along wi...
What is Constructors and Destructors in C++ (Explained with Example along wi...
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++
 
principle of oop’s in cpp
principle of oop’s in cppprinciple of oop’s in cpp
principle of oop’s in cpp
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Polymorphism in Python
Polymorphism in Python Polymorphism in Python
Polymorphism in Python
 
Access modifiers
Access modifiersAccess modifiers
Access modifiers
 
Packages in java
Packages in javaPackages in java
Packages in java
 

Semelhante a Packages access protection, importing packages

Unit4 java
Unit4 javaUnit4 java
Unit4 java
mrecedu
 

Semelhante a Packages access protection, importing packages (20)

Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
packages unit 5 .ppt
packages  unit 5 .pptpackages  unit 5 .ppt
packages unit 5 .ppt
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Packages
PackagesPackages
Packages
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
Package.pptx
Package.pptxPackage.pptx
Package.pptx
 
Java packags
Java packagsJava packags
Java packags
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Practice Program-9-Packages-Unit 4.docx
Practice Program-9-Packages-Unit 4.docxPractice Program-9-Packages-Unit 4.docx
Practice Program-9-Packages-Unit 4.docx
 
OOP_packages_222902019.pptx
OOP_packages_222902019.pptxOOP_packages_222902019.pptx
OOP_packages_222902019.pptx
 

Mais de TharuniDiddekunta

Mais de TharuniDiddekunta (17)

String class
String classString class
String class
 
Exception handling basic
Exception handling basicException handling basic
Exception handling basic
 
Creating your own exception
Creating your own exceptionCreating your own exception
Creating your own exception
 
Built in exceptions
Built in exceptions Built in exceptions
Built in exceptions
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Inheritance used in java
Inheritance used in javaInheritance used in java
Inheritance used in java
 
Operators, control statements represented in java
Operators, control statements  represented in javaOperators, control statements  represented in java
Operators, control statements represented in java
 
Classes, objects, methods, constructors, this keyword in java
Classes, objects, methods, constructors, this keyword  in javaClasses, objects, methods, constructors, this keyword  in java
Classes, objects, methods, constructors, this keyword in java
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Software Metrics (Testing)
Software Metrics (Testing)Software Metrics (Testing)
Software Metrics (Testing)
 
unit 3 Design 1
unit 3 Design 1unit 3 Design 1
unit 3 Design 1
 
Unit 4 testing
Unit 4 testingUnit 4 testing
Unit 4 testing
 
risk managment and quality
risk managment and qualityrisk managment and quality
risk managment and quality
 
Design
DesignDesign
Design
 
Network layer
Network layerNetwork layer
Network layer
 
Transport layer and Application layer
Transport layer and Application layerTransport layer and Application layer
Transport layer and Application layer
 
Congection control and Internet working
Congection control and Internet workingCongection control and Internet working
Congection control and Internet working
 

Último

AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 

Último (20)

AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 

Packages access protection, importing packages

  • 1. 3/30/2019 Packages - access protection, importing packages: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/packages-access-protection-importing-packages?module_item_id=21012860 1/2 Packages - access protec on, impor ng packages Package Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. Packages are used for: Java package is used to categorize the classes and interfaces so that they can be easily maintained. Java package provides access protection. Java package removes naming collision. All related classes are put into packages. After that we can import a class from existing packages and use it in our program. A packages is container of group of related classes where some of the classes are accessible are exposed and others are kept for internal purpose. We can reuse existing classes from the packages as many time as we need it in our program. Adding a class to a Package : We can add more classes to an created package by using package name at the top of the program and saving it in the package directory. Subpackages: Packages that are inside another package are the subpackages. 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. Types of packages: Built-in Packages: These packages consist of a large number of classes which are a part of Java API.Some of the commonly used built- in packages are: 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. User-defined packages: These are the packages that are defined by the user. First we create a directory myPackage (name should be same as the name of the package). Then create the MyClass inside the directory with the first statement being the package names. The package keyword is used to create a package in java. //save by A.java in 'pack' directory package pack; public class A { public void msg() { System.out.println("Hello"); } } There are three ways to access the package from outside the package.
  • 2. 3/30/2019 Packages - access protection, importing packages: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/packages-access-protection-importing-packages?module_item_id=21012860 2/2 1. import package.*; 2. import package.classname; 3. fully qualified name. Using packagename.* If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages. import pack.*; class Demo { public static void main(String args[]) { A obj = new A(); obj.msg(); } } Using packagename.classname If you import package.classname then only declared class of this package will be accessible. import pack.A; class Demo { public static void main(String args[]) { A obj = new A(); obj.msg(); } } Using fully qualified name If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface. It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class. class Demo { public static void main(String args[]) { A obj = new pack.A(); obj.msg(); } } Output: Hello