Object Oriented Programming Object Oriented Programming: Lecture-19 Instructor Name
Object Oriented Programming Object Oriented Programming: Lecture-19 Instructor Name
Instructor Name:
Lecture-19
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 3
short.
Package
4
Package
5
Package
Advantages of Package
Package is used to categorize the classes and interfaces so that they
can be easily maintained
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.
7
Package
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())
Every package program should be save either with public class name or
public Interface name
9
Package
10
Package
javac -d . className.java
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.
11
Package
package animals;
interface Animal {
public void eat();
public void travel();
}
12
Package
javac -d . Animal.java
javac -d . MammalInt.java
Execution
java animals.MammalInt
ammal eats
ammal travels
14
Package
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
15
Package
package mypack;
public class A {
public void show() {
System.out.println("Sum method");
}
}
16
Package
import mypack.A;
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.
18
Package
19
Package
import mypack.A;
20
String Tokenizer
Features
of
Java
Language
21
String Tokenizer
nextToken()
Which can be used to get the element from the StringTokenizer.
22
String Tokenizer
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," ");
Example of StringTokenizer
Output