SlideShare a Scribd company logo
1 of 25
Lecture-19
Instructor Name:
Object Oriented Programming
Today’s Lecture
 Packages
2
Package
What is a Package?
 A package is a collection of similar types of classes, interfaces and
sub-packages.
 A package is a namespace that organizes a set of related classes and
interfaces.
 Conceptually you can think of packages as being similar to different
folders on your computer.
 Because software written in the Java programming language can be
composed of hundreds or thousands of individual classes, it makes
sense to keep things organized by placing related classes and
interfaces into packages.
 The Java platform provides an enormous class library (a set of
packages) suitable for use in your own applications. This library is
known as the "Application Programming Interface", or "API" for
short.
3
Package
4
Package
5
Package
6
Advantages of Package
 Package is used to categorize the classes and interfaces so that they
can be easily maintained
 Application development time is less, because reuse the code
 Application memory space is less (main memory)
 Application execution time is less
 Application performance is enhance (improve)
 Redundancy (repetition) of code is minimized
 Package provides access protection.
 Package removes naming collision.
Package
7
Types of Package
 Package are classified into two type which are given below.
1. Predefined or built-in package
2. User defined package
 Predefined Packages are already designed by the Sun Microsystem
and supply as a part of java API, every predefined package is
collection of predefined classes, interfaces and sub-package.
 User Defined Packages are those which are developed by java
programmer and supply as a part of their project to deal with
common requirement.
Package
8
Rules to Create User Defined Package
 package statement should be the first statement of any package program.
 Choose an appropriate class name or interface name and whose modifier
must be public.
 Any package program can contain only one public class or only one public
interface but it can contain any number of normal classes.
 Package program should not contain any main class (that means it should not
contain any main())
 modifier of constructor of the class which is present in the package must be
public. (This is not applicable in case of interface because interface have no
constructor.)
Package
9
Rules to Create User Defined Package
 The modifier of method of class or interface which is present in the package
must be public (This rule is optional in case of interface because interface
methods by default public)
 Every package program should be save either with public class name or
public Interface name
Package
10
Package
11
Compiling a Package Program
 For compilation of package program first we save program with public
className.java and it compile using below syntax:
javac -d . className.java
avac -d path className.java
 In above syntax "-d" is a specific tool which is tell to java compiler create a
separate folder for the given package in given path.
 When we give specific path then it create a new folder at that location and
when we use . (dot) then it crate a folder at current working directory.
Package
12
Example of Package Program
Below given package example contains interface named animals:
/* File name : Animal.java */
package animals;
interface Animal {
public void eat();
public void travel();
}
Package
13
Example of Package Program
package animals;
/* File name : MammalInt.java */
public class MammalInt implements Animal{
public void eat(){
System.out.println("Mammal eats");
}
public void travel(){
System.out.println("Mammal travels");
}
public int noOfLegs(){
return 0;
}
public static void main(String args[]){
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
Package
14
Compiling and Executing
Compile the java file as shown below
javac -d . Animal.java
javac -d . MammalInt.java
Execution
java animals.MammalInt
ammal eats
ammal travels
Package
15
Import keyword
 If a class wants to use another class in the same package, the package
name does not need to be used.
 Classes in the same package find each other without any special
syntax.
 Import keyword is used to include any package
Package
16
Example of Package Program with import
 Package program which is save with A.java and compile by javac -d . A.java
package mypack;
public class A {
public void show() {
System.out.println("Sum method");
}
}
Package
17
Example of Package Program with import
 Import above class in below program using import satatement
import mypack.A;
public class Hello {
public static void main(String arg[]) {
A a=new A();
a.show()
System.out.println("show() class A");
}
}
Package
18
Difference between Inheritance & Package
 Inheritance concept always used to reuse the feature within the
program between class to class, interface to interface and interface to
class but not accessing the feature across the program.
 Package concept is to reuse the feature both within the program and
across the programs between class to class, interface to interface and
interface to class.
Package
19
package keyword vs import keyword
 package keyword is always used for creating the undefined package
and placing common classes and interfaces.
 import is a keyword which is used for referring or using the classes
and interfaces of a specific package.
Package
20
Directory Structure of packages
 Two major results occur when a class is placed in a package:
1. The name of the package becomes a part of the name of the class,
as we have seen in the previous code.
import mypack.A;
2. The name of the package must match the directory structure
where the corresponding bytecode resides.
String Tokenizer
21
What is String Tokenizer?
 It is a pre defined class in java.util package can be used to split the
given string into tokens (parts) based on delimiters (any special
symbols or spaces).
 Suppose that we have any string like "Features of Java_Language"
when we use stringTokenizer this string is split into tokens whenever
spaces and special symbols present.
 After split string are :
Features
of
Java
Language
String Tokenizer
22
Methods of String Tokenizer
 hasMoreTokens()
It is predefined method of StringTokenizer class used to check whether
given StringTokenizer having any elements or not.
 nextToken()
Which can be used to get the element from the StringTokenizer.
String Tokenizer
23
Example of StringTokenizer
import java.util.*;
class Stringtokenizerdemo {
public static void main(String args[]) {
String str="He is a gentle man";
StringTokenizer st=new StringTokenizer(str," ");
System.out.println("The tokens are: ");
while(st.hasMoreTokens()) {
String one=st.nextToken();
System.out.println(one);
}
}
}
String Tokenizer
24
Example of StringTokenizer
Output
The tokens are:
He
is
a
gentle
man
25

More Related Content

What's hot

What's hot (20)

Java package
Java packageJava package
Java package
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java packages
Java packagesJava packages
Java packages
 
Packages,interfaces and exceptions
Packages,interfaces and exceptionsPackages,interfaces and exceptions
Packages,interfaces and exceptions
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
5.interface and packages
5.interface and packages5.interface and packages
5.interface and packages
 
Packages in java
Packages in javaPackages in java
Packages in java
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
 
Java packages
Java packagesJava packages
Java packages
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java program structure
Java program structureJava program structure
Java program structure
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
 
java packages
java packagesjava packages
java packages
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Packages
PackagesPackages
Packages
 
Interface
InterfaceInterface
Interface
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Pi j4.1 packages
Pi j4.1 packagesPi j4.1 packages
Pi j4.1 packages
 

Similar to Lecture 19

Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages ConceptsVicter Paul
 
Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)Narayana Swamy
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packagesKuntal Bhowmick
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packagesTharuniDiddekunta
 
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
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packagesKuntal Bhowmick
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packagesmanish kumar
 
Power point presentation on access specifier in OOPs
Power point presentation on access specifier in OOPsPower point presentation on access specifier in OOPs
Power point presentation on access specifier in OOPsAdrizaBera
 
Lecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.pptLecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.pptlirogal422
 

Similar to Lecture 19 (20)

Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)
 
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
 
Java packages
Java packagesJava packages
Java packages
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packages
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
150950107056 2150704
150950107056 2150704150950107056 2150704
150950107056 2150704
 
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
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
 
Power point presentation on access specifier in OOPs
Power point presentation on access specifier in OOPsPower point presentation on access specifier in OOPs
Power point presentation on access specifier in OOPs
 
Lecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.pptLecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.ppt
 
Java packags
Java packagsJava packags
Java packags
 
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
 

More from talha ijaz

More from talha ijaz (16)

Lecture 23-24.pptx
Lecture 23-24.pptxLecture 23-24.pptx
Lecture 23-24.pptx
 
Lecture 22
Lecture 22Lecture 22
Lecture 22
 
Lecture 20-21
Lecture 20-21Lecture 20-21
Lecture 20-21
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Visual perception
Visual perceptionVisual perception
Visual perception
 
Lecture 11
Lecture 11Lecture 11
Lecture 11
 
Introduction
IntroductionIntroduction
Introduction
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 

Recently uploaded

PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineeringssuserb3a23b
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 

Recently uploaded (20)

PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineering
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 

Lecture 19

  • 3. Package What is a Package?  A package is a collection of similar types of classes, interfaces and sub-packages.  A package is a namespace that organizes a set of related classes and interfaces.  Conceptually you can think of packages as being similar to different folders on your computer.  Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.  The Java platform provides an enormous class library (a set of packages) suitable for use in your own applications. This library is known as the "Application Programming Interface", or "API" for short. 3
  • 6. Package 6 Advantages of Package  Package is used to categorize the classes and interfaces so that they can be easily maintained  Application development time is less, because reuse the code  Application memory space is less (main memory)  Application execution time is less  Application performance is enhance (improve)  Redundancy (repetition) of code is minimized  Package provides access protection.  Package removes naming collision.
  • 7. Package 7 Types of Package  Package are classified into two type which are given below. 1. Predefined or built-in package 2. User defined package  Predefined Packages are already designed by the Sun Microsystem and supply as a part of java API, every predefined package is collection of predefined classes, interfaces and sub-package.  User Defined Packages are those which are developed by java programmer and supply as a part of their project to deal with common requirement.
  • 8. Package 8 Rules to Create User Defined Package  package statement should be the first statement of any package program.  Choose an appropriate class name or interface name and whose modifier must be public.  Any package program can contain only one public class or only one public interface but it can contain any number of normal classes.  Package program should not contain any main class (that means it should not contain any main())  modifier of constructor of the class which is present in the package must be public. (This is not applicable in case of interface because interface have no constructor.)
  • 9. Package 9 Rules to Create User Defined Package  The modifier of method of class or interface which is present in the package must be public (This rule is optional in case of interface because interface methods by default public)  Every package program should be save either with public class name or public Interface name
  • 11. Package 11 Compiling a Package Program  For compilation of package program first we save program with public className.java and it compile using below syntax: javac -d . className.java avac -d path className.java  In above syntax "-d" is a specific tool which is tell to java compiler create a separate folder for the given package in given path.  When we give specific path then it create a new folder at that location and when we use . (dot) then it crate a folder at current working directory.
  • 12. Package 12 Example of Package Program Below given package example contains interface named animals: /* File name : Animal.java */ package animals; interface Animal { public void eat(); public void travel(); }
  • 13. Package 13 Example of Package Program package animals; /* File name : MammalInt.java */ public class MammalInt implements Animal{ public void eat(){ System.out.println("Mammal eats"); } public void travel(){ System.out.println("Mammal travels"); } public int noOfLegs(){ return 0; } public static void main(String args[]){ MammalInt m = new MammalInt(); m.eat(); m.travel(); } }
  • 14. Package 14 Compiling and Executing Compile the java file as shown below javac -d . Animal.java javac -d . MammalInt.java Execution java animals.MammalInt ammal eats ammal travels
  • 15. Package 15 Import keyword  If a class wants to use another class in the same package, the package name does not need to be used.  Classes in the same package find each other without any special syntax.  Import keyword is used to include any package
  • 16. Package 16 Example of Package Program with import  Package program which is save with A.java and compile by javac -d . A.java package mypack; public class A { public void show() { System.out.println("Sum method"); } }
  • 17. Package 17 Example of Package Program with import  Import above class in below program using import satatement import mypack.A; public class Hello { public static void main(String arg[]) { A a=new A(); a.show() System.out.println("show() class A"); } }
  • 18. Package 18 Difference between Inheritance & Package  Inheritance concept always used to reuse the feature within the program between class to class, interface to interface and interface to class but not accessing the feature across the program.  Package concept is to reuse the feature both within the program and across the programs between class to class, interface to interface and interface to class.
  • 19. Package 19 package keyword vs import keyword  package keyword is always used for creating the undefined package and placing common classes and interfaces.  import is a keyword which is used for referring or using the classes and interfaces of a specific package.
  • 20. Package 20 Directory Structure of packages  Two major results occur when a class is placed in a package: 1. The name of the package becomes a part of the name of the class, as we have seen in the previous code. import mypack.A; 2. The name of the package must match the directory structure where the corresponding bytecode resides.
  • 21. String Tokenizer 21 What is String Tokenizer?  It is a pre defined class in java.util package can be used to split the given string into tokens (parts) based on delimiters (any special symbols or spaces).  Suppose that we have any string like "Features of Java_Language" when we use stringTokenizer this string is split into tokens whenever spaces and special symbols present.  After split string are : Features of Java Language
  • 22. String Tokenizer 22 Methods of String Tokenizer  hasMoreTokens() It is predefined method of StringTokenizer class used to check whether given StringTokenizer having any elements or not.  nextToken() Which can be used to get the element from the StringTokenizer.
  • 23. String Tokenizer 23 Example of StringTokenizer import java.util.*; class Stringtokenizerdemo { public static void main(String args[]) { String str="He is a gentle man"; StringTokenizer st=new StringTokenizer(str," "); System.out.println("The tokens are: "); while(st.hasMoreTokens()) { String one=st.nextToken(); System.out.println(one); } } }
  • 24. String Tokenizer 24 Example of StringTokenizer Output The tokens are: He is a gentle man
  • 25. 25