SlideShare una empresa de Scribd logo
1 de 26
Packages and Classpath


Objective :

  On completion of this period, you would be
  able to learn
  • Packages
  • Concept of classpath




                                               1
Recap



Final class
• Classes that can not be inherited




                                      2
Packages

• What is a package?
  • We already know how to organize our
    files
  • We use directories for doing so
  • The advantage is
      • Easy maintenance of files
  • If we want to organize our java class files
      • System provides a facility
         • Packages

                                                  3
Packages    Contd..


• Package
  • Name provided to a group of classes
  • A collection of related classes and
    interfaces
  • A namespace management tool provided
    by Java



                                           4
Packages       Contd..
• Directory structures that organize classes and
  interfaces
• Mechanism for software reuse
• Can be compared to directories or folders


• Files : Directories :: Class files : Packages




                                                   5
                                                       5
Naming Convention

•   Follows standard Java naming rules
•   Case sensitive
•   Generally written in small case
•   Every name must be unique
•   Packages in the Java language itself begin with
    java




                                                  6
Naming Convention Contd..

• Prefix a domain name to the preferred package
  name
   • Avoids name conflict when used in Internet
   • for example, com.company.region.package
      • com.ibm.in.banking




                                                  7
Packages


Java packages are classified in to two types :
• Java API packages ( system defined packages)
• User defined packages




                                                 8
Packages       Contd..
•The packages are organized in a hierarchical structure as
shown in the figure

          java. awt

                               Package containing awt package
          COLOUR

         GRAPHICS
                                Package containing AWT classes

           FONT
            …




          IMAGE                   AWT classes containg methods
                          Hierarchical representation of java.awt
                          package. The package named java contains the
                          package awt , which in turn contains various
        Fig. 26.1         classes required for implementing graphical
                          user interface
                                                                   9
System defined packages
• Some examples
  • java.io
  • java.util
  • java.lang
  • java.awt
  • java.awt.event
  • java.sql
  • java.net
  • java.math
  • java.io
                                   10
Package Statement
• Used to create a package
• Should be the first statement in a Java source
  file
• Syntax
  – package nameOfPackage;

  Keyword
                 User-defined name


 • eg. : package cis; // cis – college information system
 • nameOfPackage is a sequence of names separated by .
 (dot)

                                                            11
Package Statement Contd..
• Java supports the package hierarchy
• Specified by multiple names in a package
  statement, separated by . (dot)

  – eg. : package firstPackage.secondPackage;

• Advantage is
  – Group related classes into a package and
    then group the related packages into a larger
    package
                                                12
Packages
Example :
package firstPpackage ; // package declanation
public class firstClass // class definition
{
    ----------- ( body of class )
    -----------
 }
• Here the package name is ‘firstPackage’
• The class ‘firstClass’ is now considered a part of this
   package
• This listing would be saved as a file called firstClass.java
• When the source file is compiled, java compiler will
  create a class file and store it in the ‘firstPackage’
  directory                                            13
Example Program
// A simple package
package coreBank;

class Balance {                  package statement

 String name;
 double bal;

 Balance(String n, double b) {
   name = n;
   bal = b;
 }
                                                     14
Example Program Contd..
void show() {
    if(bal<0)
      System.out.print("-->> ");
      System.out.println(name + ": $" + bal);
  }
}




                                                15
Example Program Contd..
 package coreBank;
 public class AccountBalance {
    public static void main(String args[]) {
      Balance current[] = new Balance[3];
      current[0] = new Balance("K. J. Fielding", 123.23);
      current[1] = new Balance("Will Tell", 157.02);
      current[2] = new Balance("Tom Jackson", -12.33);
      for(int i=0; i<3; i++) current[i].show();
  }
}



                                                      16
Compiling and Executing
• On compilation
  • The package statement creates
    directory(folder) with the name of the package
  • That is, folder named ‘coreBank’ is created
• While executing
  • Qualify the class name with its package name
  • That is, coreBank. AccountBalance




                                                17
Compiling and Executing                   Contd..

• Compiling
  • javac AccountBalance.java

• Executing
  • java coreBank.AccountBalance


         Class name to be qualified with its package




                                                         18
Default Package

• System provides when you do not create one
• It is the current working directory




                                               19
Advantages of Packages


• Easily determine how classes and
  interfaces are related
• Avoids namespace conflict
• Implement       application-wise  access
  protection
• Provides a mechanism for class reuse




                                             20
Summary
• In this class we have discussed
   • Package
   • Naming convention
   • Creating a package
   • Concepts of classpath




                                    21
Frequently Asked Questions
1. What is a package ?

2. What are Java’s system defined packages ?

3. What are the advantages of packages ?

4. How to set classpath for your Java
   application ?


                                           22
Quiz

1.An package is a collection of

  1. classes
  2. Interfaces
  3. editing tools
  4. classes and interfaces




                                  23
Quiz             Contd..




2.Which keyword is used to define package

  1. packages
  2. package
  3. packaging
  4. directory




                                            24
Quiz               Contd..




3.A class path specifies

  1. A list of paths for java source files
  2. A list of paths for image files
  3. A list of paths for the required classes and
     zip files
  4. None



                    CM604.26                     25
Quiz              Contd..

4.Which of the following statements is true ?

  1. There can be any number of package
     statements in a java file
  2. Package statement can be any where in the
     java file
  3. There can be only one package statement in
     a java file
  4. None



                      CM604.26                    26

Más contenido relacionado

La actualidad más candente

Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
Satya Johnny
 
chap 10 : Development (scjp/ocjp)
chap 10 : Development (scjp/ocjp)chap 10 : Development (scjp/ocjp)
chap 10 : Development (scjp/ocjp)
It Academy
 
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa Aptech
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa AptechSession 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa Aptech
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa Aptech
MasterCode.vn
 

La actualidad más candente (20)

Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Packages in java
Packages in javaPackages in java
Packages in java
 
OCA JAVA - 1 Packages and Class Structure
 OCA JAVA - 1 Packages and Class Structure OCA JAVA - 1 Packages and Class Structure
OCA JAVA - 1 Packages and Class Structure
 
Java packages
Java packagesJava packages
Java packages
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packages
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access ModifiersOOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
chap 10 : Development (scjp/ocjp)
chap 10 : Development (scjp/ocjp)chap 10 : Development (scjp/ocjp)
chap 10 : Development (scjp/ocjp)
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
 
Java Inheritance | Java Course
Java Inheritance | Java Course Java Inheritance | Java Course
Java Inheritance | Java Course
 
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa Aptech
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa AptechSession 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa Aptech
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa Aptech
 
Java basics
Java basicsJava basics
Java basics
 
Introduction to OSGi
Introduction to OSGiIntroduction to OSGi
Introduction to OSGi
 
Java Tutorial 1
Java Tutorial 1Java Tutorial 1
Java Tutorial 1
 

Destacado (11)

Ap Power Point Chpt5
Ap Power Point Chpt5Ap Power Point Chpt5
Ap Power Point Chpt5
 
Ap Power Point Chpt7
Ap Power Point Chpt7Ap Power Point Chpt7
Ap Power Point Chpt7
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 B
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4
 
Java pdf
Java   pdfJava   pdf
Java pdf
 
Ap Power Point Chpt9
Ap Power Point Chpt9Ap Power Point Chpt9
Ap Power Point Chpt9
 
Ap Power Point Chpt2
Ap Power Point Chpt2Ap Power Point Chpt2
Ap Power Point Chpt2
 
Ap Power Point Chpt6
Ap Power Point Chpt6Ap Power Point Chpt6
Ap Power Point Chpt6
 
Ap Power Point Chpt1
Ap Power Point Chpt1Ap Power Point Chpt1
Ap Power Point Chpt1
 
OCA JAVA - 2 Programming with Java Statements
 OCA JAVA - 2 Programming with Java Statements OCA JAVA - 2 Programming with Java Statements
OCA JAVA - 2 Programming with Java Statements
 
Core Java Slides
Core Java SlidesCore Java Slides
Core Java Slides
 

Similar a Packages(9 cm604.26)

Unit4 java
Unit4 javaUnit4 java
Unit4 java
mrecedu
 

Similar a Packages(9 cm604.26) (20)

Java Packages
Java Packages Java Packages
Java Packages
 
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
PackagesPackages
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 .
 
Pi j4.1 packages
Pi j4.1 packagesPi j4.1 packages
Pi j4.1 packages
 
javapackage
javapackagejavapackage
javapackage
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Package.pptx
Package.pptxPackage.pptx
Package.pptx
 
Chap1 packages
Chap1 packagesChap1 packages
Chap1 packages
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
Chapter 1 :
Chapter 1 : Chapter 1 :
Chapter 1 :
 
OOP_packages_222902019.pptx
OOP_packages_222902019.pptxOOP_packages_222902019.pptx
OOP_packages_222902019.pptx
 
OOP_packages_222902019.pptx
OOP_packages_222902019.pptxOOP_packages_222902019.pptx
OOP_packages_222902019.pptx
 
Java packags
Java packagsJava packags
Java packags
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
 
20-packages-jar.ppt
20-packages-jar.ppt20-packages-jar.ppt
20-packages-jar.ppt
 

Más de myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Último

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Último (20)

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

Packages(9 cm604.26)

  • 1. Packages and Classpath Objective : On completion of this period, you would be able to learn • Packages • Concept of classpath 1
  • 2. Recap Final class • Classes that can not be inherited 2
  • 3. Packages • What is a package? • We already know how to organize our files • We use directories for doing so • The advantage is • Easy maintenance of files • If we want to organize our java class files • System provides a facility • Packages 3
  • 4. Packages Contd.. • Package • Name provided to a group of classes • A collection of related classes and interfaces • A namespace management tool provided by Java 4
  • 5. Packages Contd.. • Directory structures that organize classes and interfaces • Mechanism for software reuse • Can be compared to directories or folders • Files : Directories :: Class files : Packages 5 5
  • 6. Naming Convention • Follows standard Java naming rules • Case sensitive • Generally written in small case • Every name must be unique • Packages in the Java language itself begin with java 6
  • 7. Naming Convention Contd.. • Prefix a domain name to the preferred package name • Avoids name conflict when used in Internet • for example, com.company.region.package • com.ibm.in.banking 7
  • 8. Packages Java packages are classified in to two types : • Java API packages ( system defined packages) • User defined packages 8
  • 9. Packages Contd.. •The packages are organized in a hierarchical structure as shown in the figure java. awt Package containing awt package COLOUR GRAPHICS Package containing AWT classes FONT … IMAGE AWT classes containg methods Hierarchical representation of java.awt package. The package named java contains the package awt , which in turn contains various Fig. 26.1 classes required for implementing graphical user interface 9
  • 10. System defined packages • Some examples • java.io • java.util • java.lang • java.awt • java.awt.event • java.sql • java.net • java.math • java.io 10
  • 11. Package Statement • Used to create a package • Should be the first statement in a Java source file • Syntax – package nameOfPackage; Keyword User-defined name • eg. : package cis; // cis – college information system • nameOfPackage is a sequence of names separated by . (dot) 11
  • 12. Package Statement Contd.. • Java supports the package hierarchy • Specified by multiple names in a package statement, separated by . (dot) – eg. : package firstPackage.secondPackage; • Advantage is – Group related classes into a package and then group the related packages into a larger package 12
  • 13. Packages Example : package firstPpackage ; // package declanation public class firstClass // class definition { ----------- ( body of class ) ----------- } • Here the package name is ‘firstPackage’ • The class ‘firstClass’ is now considered a part of this package • This listing would be saved as a file called firstClass.java • When the source file is compiled, java compiler will create a class file and store it in the ‘firstPackage’ directory 13
  • 14. Example Program // A simple package package coreBank; class Balance { package statement String name; double bal; Balance(String n, double b) { name = n; bal = b; } 14
  • 15. Example Program Contd.. void show() { if(bal<0) System.out.print("-->> "); System.out.println(name + ": $" + bal); } } 15
  • 16. Example Program Contd.. package coreBank; public class AccountBalance { public static void main(String args[]) { Balance current[] = new Balance[3]; current[0] = new Balance("K. J. Fielding", 123.23); current[1] = new Balance("Will Tell", 157.02); current[2] = new Balance("Tom Jackson", -12.33); for(int i=0; i<3; i++) current[i].show(); } } 16
  • 17. Compiling and Executing • On compilation • The package statement creates directory(folder) with the name of the package • That is, folder named ‘coreBank’ is created • While executing • Qualify the class name with its package name • That is, coreBank. AccountBalance 17
  • 18. Compiling and Executing Contd.. • Compiling • javac AccountBalance.java • Executing • java coreBank.AccountBalance Class name to be qualified with its package 18
  • 19. Default Package • System provides when you do not create one • It is the current working directory 19
  • 20. Advantages of Packages • Easily determine how classes and interfaces are related • Avoids namespace conflict • Implement application-wise access protection • Provides a mechanism for class reuse 20
  • 21. Summary • In this class we have discussed • Package • Naming convention • Creating a package • Concepts of classpath 21
  • 22. Frequently Asked Questions 1. What is a package ? 2. What are Java’s system defined packages ? 3. What are the advantages of packages ? 4. How to set classpath for your Java application ? 22
  • 23. Quiz 1.An package is a collection of 1. classes 2. Interfaces 3. editing tools 4. classes and interfaces 23
  • 24. Quiz Contd.. 2.Which keyword is used to define package 1. packages 2. package 3. packaging 4. directory 24
  • 25. Quiz Contd.. 3.A class path specifies 1. A list of paths for java source files 2. A list of paths for image files 3. A list of paths for the required classes and zip files 4. None CM604.26 25
  • 26. Quiz Contd.. 4.Which of the following statements is true ? 1. There can be any number of package statements in a java file 2. Package statement can be any where in the java file 3. There can be only one package statement in a java file 4. None CM604.26 26