0% found this document useful (0 votes)
25 views

10 Packages Java

This document discusses packages in Java. It defines that a package is a collection of related classes and interfaces that provides namespace management and access protection. There are two types of packages - built-in packages that are part of the Java API, and user-defined packages that can be created. Advantages of packages include organization, access protection, and avoiding naming collisions. The document explains how to create a user-defined package with an example and the different ways to access packages using fully qualified names or imports.

Uploaded by

AAHANA MANNI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

10 Packages Java

This document discusses packages in Java. It defines that a package is a collection of related classes and interfaces that provides namespace management and access protection. There are two types of packages - built-in packages that are part of the Java API, and user-defined packages that can be created. Advantages of packages include organization, access protection, and avoiding naming collisions. The document explains how to create a user-defined package with an example and the different ways to access packages using fully qualified names or imports.

Uploaded by

AAHANA MANNI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

PACKAGES IN JAVA

Vedprakash Sharma
Assistant Professor
IT Dept.
ADGITM

Vedprakash Sharma, IT Dept., ADGITM 1


CONTENTS
 Introduction of Packages
 Advantages Of Packages
 Types of Packages
1. Built in Packages
2. User Defined Packages
 Steps For Creating Package
 Accessing a Package
 Sub-packages in java

2
INTRODUCTION
 A Package is a collection of related Java entities (such
as classes, interfaces, folders/subpackages) to provide
access protection and name space management.
 Packages act as “containers” for related java type
files.
 A package provides a mechanism for grouping a variety
of similar types of classes, interfaces and sub-
packages.
 Example of existing packages:
Java.util
Java.awt

3
ADVANTAGE OF JAVA PACKAGE
1) Java package is used to categorize the classes and
interfaces so that they can be easily maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
 com.zzz.Circle and com.yyy.Circle are two distinct
classes.
4) Packages hide the classes & interfaces in a
separate subdirectory, so that accidental deletion
of classes & interfaces will not take place.
5) Reusability

4
TYPES OF PACKAGES

1. Built-in package
These are also known as System Packages or Java
API.
such as java, lang, awt, javax, swing, net, io,
util, sql etc.
2. User-defined package
 The users of the Java language can also create
their own packages. They are called user-defined
packages.
 User defined packages can also be imported into
other classes & used exactly in the same way as
the Built in packages.
5
BUILT-IN PACKAGE
 As there are built in methods , java also provides inbuilt
packages which contain lots of classes & interfaces. These
classes inside the packages are already defined & we can use
them by importing relevant package in our program.

 Java has an extensive library of packages, a programmer need


not think about logic for doing any task.

6
Java

util lang awt

Scanner.classs ArrayList.class System.class String.class TextField.class Button.class

7
JAVA.AWT.COLOR;
JAVA.AWT.GRAPHICS;
java

Package
awt
containing awt
package
Color
Package
containing classes
Graphics
a

Image

8
BUILT-IN PACKAGE EXAMPLES
 java.lang
Language Support classes. These are classes that java compiler itself uses & therefore
they are automatically imported. They include classes for primitive types, strings,
maths function, threads &exception.
 java .util
Language Utility classes such as vector, hash tables ,random numbers, date etc.
 java.io
Input /Output support classes. They provide facilities for the input & output of data
 java.awt
Set of classes for implementing graphical user interface. They include classes for
windows, buttons, list, menus & so on.
 java.net
Classes for networking. They include classes for communicating with local computers
as well as with internet servers.
 java.applet
Classes for creating & implementing applets.

9
ACCESS THE BUILT IN PACKAGE
 There are three ways to access the package
from outside the package.
1. fully qualified name.
2. import package.Classname;
3. import package.*;

10
USER DEFINED PACKAGES

Vehicle

Car Bike

Bmw Ferrari Ford Honda yamaha

11
USER DEFINED PACKAGES
To create a user defined package the following steps should be involved :-
1. Declare the package at the beginning of a file using
syntax :- package packageName;
2. Define the class that is to be put in the package & declare it public.
3. Create a subdirectory under the directory where the main source files are
stored.
4. Store the listing as the classname.java file in the subdirectory created.
5. Compile the file. This create .class file in the subdirectory.
6. Run the file.

12
CONTINUED…
1. Creating User Defined Packages
 Syntax :
package packageName;
public class className
{
-------------
// Body of className
------------
}
 Package declaration statement must be the first line in the program
 Package name should be unique and in lowercase.
 How to compile java package
 If you are not using any IDE, you need to follow the syntax given below:
javac -d directory javafilename 
 The -d is a switch that tells the compiler where to put the class file i.e. it
represents destination. The . represents the current folder.

13
EXAMPLE
package vehicle;  
public class Bmw{  
 public void display()
{  
    System.out.println(“Hello from Bmw class");  
   }  
}  

 compile
javac -d . Bmw.java
 Execute
java vehicle.car.Bmw
 Result
Hello from Bmw class

14
CONTINUE… IN THE ABOVE EXAMPLE
■ vehicle is the name of the package.
■ The class Bmw is now considered as a part of this
package.
■ This listing would be saved as a file called Bmw.java &
located in a directory named vehicle.
■ When the source file is compiled, java will create a .class
file & store it in the same directory.
■ The .class files must be located in a directory that has
the same name as the package & this directory should be
a subdirectory of the directory where classes that will
import the package are located.

15
ACCESS THE PACKAGE
 There are three ways to access the package
from outside the package.
1. fully qualified name.
2. import package.Classname;
3. import package.*;

16
1) 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.

//saves with Demo.java


class Demo{  
  public static void main(String args[])
{  
  vehicle.Bmw obj = new  vehicle.Bmw();
   obj.display();  
   }  

17
2) USING PACKAGENAME.CLASSNAME
 If you import package.classname then only declared class of
this package will be accessible.
 The import keyword is used to make the classes and interface
of another package accessible to the current package.

//Example of built in package


import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter your name: “);
String name = sc.nextLine();
System.out.println("Name: "+name);
}
}

18
EXAMPLE OF USER DEFINED PACKAGE
//save by Demo2.java  
import  vehicle.Bmw;  
  class Demo2{  
  public static void main(String args[])
{  
   Bmw obj = new Bmw();  
   obj.display();  
  }   >Javac –d . Bmw.java

} >Javac Demo2.java

>Java Demo2

19
3) IMPORT PACKAGE.*;

 If you use package.* then all the classes and interfaces of this package will be
accessible but not sub-packages.
 Used only when there are multiple classes in a package

//save by Demo3.java  
  import  vehicle.*;  
  class Demo2{  
  public static void main(String args[])
{  
   Bmw obj = new Bmw();  
   obj.display();  
  }  
}

20
THANK YOU

Vedprakash Sharma, IT Dept., ADGITM 21

You might also like