0% found this document useful (0 votes)
11 views33 pages

8 Package

Uploaded by

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

8 Package

Uploaded by

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

Package

Packages
 Provides a mechanism for grouping a variety of
classes and / or interfaces together.
 Grouping is based on functionality.
Benefits:
 The classes contained in the packages of other
programs can be reused.
 In packages, classes can be unique compared with
classes in other packages.
 Packages provides a way to hide classes.
Packages
 Two types of packages: 1. Java API packages 2. User defined
packages
Java API Packages:
 A large number of classes grouped into different packages
based on functionality. Examples:
1. java.lang
2. java.util
3. java.io
4. java.awt
5.java.net
6.java. applet etc.
Package
Java
Package containing awt package
awt

Color Package containing classes

Graphics

Image
Accessing Classes in a Package
1. Fully Qualified class name:
Example:java.awt.Color
2. import packagename.classname;
Example: import java.awt.Color;
or
import packagename.*;
Example: import java.awt.*;
 Import statement must appear at the top of the file,
before any class declaration.
Creating Your Own Package
1. Declare the package at the
beginning of a file using the form
Example:
package packagename; package firstPackage;
2. Define the class that is to be put in
the package and declare it public.
3. Create a subdirectory under the Public class FirstClass
directory where the main source
files are stored. {
4. Store the listing as classname.java
in the subdirectory created. //Body of the class
5. Compile the file. This creates .class }
file in the subdirectory.
Example1-Package
package p1; import p1.*;
public class ClassA
{ Class testclass
public void displayA( ) {
{ public static void main(String str[])
System.out.println(“Class A”); {
} ClassA obA=new ClassA();
} obA.displayA();
}
Source file – ClassA.java
}
Subdirectory-p1 Source file-testclass.java
ClassA.Java and ClassA.class->p1 testclass.java and testclass.class->in
a directory of which p1 is
subdirectory.
Sequence of the program must be package then import then class.

12/18/24
Creating Packages

 Consider the following declaration:


package firstPackage.secondPackage;
This package is stored in subdirectory named
firstPackage.secondPackage.
 A java package can contain more than one class
definitions that can be declared as public.
 Only one of the classes may be declared public and
that class name with .java extension is the source file
name.
Example2-Package
package Mine; import Mine.*;
//New package declared //Package is imported
by user class Mpack
public class packs {
{ public static void main(String args[])
public void display() {
{ packs d=new packs();
System.out.println("Welcome to // Object of Package class is
World of Java"); created
} d.display();
} //Method of our Package is
accessed
}
}
Example 3- Package
import p1.ClassA;
package p1;
import Mine.*;
class mtestclass
public class ClassA
{
{
public static void main(String args[])
public void displayA()
{
{
ClassA ob=new ClassA();
ob.displayA();
System.out.println("classA");
packs d=new packs();
// Object of Package class is created
}
d.display();
}
//Method of our Package is accessed
}
}
Package
package p1; Correct Code:
public class Teacher
{………….} import p1.*;
public class Student import p2.*;
{……………..}

package p2; p1.Student student1;


public class Courses p2.Student student2;
{………..}
public class Student
{………………..}
import p1.*;
import p2.*;
Student student1; //Error
CLASSPATH Environment
Variable
 The compiler and runtime interpreter know how to
find standard packages such as java.lang and java.util
 The CLASSPATH environment variable is used to
direct the compiler and interpreter to where
programmer defined imported packages can be found
 The CLASSPATH environment variable is an
ordered list of directories and files
CLASSPATH Environment Variable

 To set the CLASSPATH variable we use the


following command:
set CLASSPATH=c:\
 Java compiler and interpreter searches the user
defined packages from the above directory.
 To clear the previous setting we use:
set CLASSPATH=
Example1-Package[Using CLASSPATH]
import p1.ClassA;
package p1;
public class ClassA
Class PackageTest1
{ {
public void displayA( ) public static void main(String str[])
{ {
System.out.println(“Class A”); ClassA obA=new ClassA();
} obA.displayA();
} }}
Source file-
Source file – c:\p1\ClassA.java
c:\java\jdk1.6.0_06\bin\PackageTest1.java

Compile-javac c:\p1\ClassA.java
Compile-javac PackageTest1.java
Copy –PackageTest1.class -> c:\
Class file in –c:\p1\ClassA.class
Execute-java PackageTest1
Adding a Class to a Package
 Every java source file can contain only class declared as
public.
 The name of the source file should be same as the name of the
public class with .java extension.

package p1; package p1;


public public ClassB{…………}
ClassA{ ……………}
Source file: ClassB.java
Source file :
ClassA.java Subdirectory:p1
Subdirectory: p1
Adding a Class to a Package
1.Decide the name of the package.
2.Create the subdirectory with this name under
the directory where the main source file is
located.
3.Create classes to be placed in the package in
separate source files and declare the package
statement
package packagename;
4. Compile each source file. When completed the
package will contain .class files of the source files.
Access within within outside outside
Modifier class package package package
by
subclass
only
private YES NO NO NO
default YES YES NO NO
protected YES YES YES NO
public YES YES YES YES

12/18/24 18
public/package/private scope
 Scope is concerned with the visibility of program
elements such as classes and members
 Class members (methods or instance fields) can be
defined with public, package (default), private or
protected scope
 A class has two levels of visibility:
-public scope means it is visible outside its containing
package
- default scope means it is visible only inside the
package. (package scope/ friendly scope)
public/package/private scope
 A class member with public scope means it is visible
anywhere its class is visible

 A class member with private scope means it is visible


only within its encapsulating class

 A class/class member with package scope means it is


visible only inside its containing package

 A class member with protected scope means it is visible


every where except the non-subclasses in other package.
default access modifier
If you don't use any modifier, it is treated as default bydefault. The
default modifier is accessible only within package.

Example of default access modifier


Default: When no access modifier is specified for a class ,
method or data member – It is said to be having
the defaultaccess modifier by default.

The data members, class or methods which are not declared


using any access modifiers i.e. having default access modifier
are accessible only within the same package.

we will create two packages and the classes in the packages will
be having the default access modifiers and we will try to access a
class from one package from a class of second package.
Program in which you will
declare a default method in
the base class of a different
package and use that method
in the child class of a different
package.

12/18/24 23
package p1;
public class class_in_package
{
void print1()
{
System.out.println("message in package class");
}
public void show()
{
print1();
}
}

12/18/24 24
import p1. class_in_package;
class lakshay extends class_in_package
{
void display()
{
class_in_package mca = new class_in_package ();
mca.show();
}
public static void main (String args[])
{
lakshay l_128 = new lakshay();
l_128.display();
}
}
12/18/24 25
Private access modifier

The private access modifier is specified using the keyword private.

The methods or data members declared as private are accessible


only within the class in which they are declared.

Any other class of same package will not be able to


access these members.

Classes or interface can not be declared as private.


class x
{
private void display()
{
System.out.println("Method in private class");
}
}
class privateclass
{
public static void main(String args[])
{
x obj = new x();
// Trying to access private method
// of another class
obj.display();
}
}
12/18/24 SOBUZ 27
class x
{
void display()
{
System.out.println("Method in private class");
}
}
class privateclass
{
public static void main(String args[])
{
x obj = new x();
// Trying to access private method
// of another class
obj.display();
}
}
12/18/24 SOBUZ 28
package privatepackage;
class x {
private void display()
{
System.out.println("Method in private class");
}
}
class privateclass
{
public static void main(String args[])
{
x obj = new x();
// Trying to access private method
// of another class
obj.display();
}
}
12/18/24 SOBUZ 29
Protected access modifier
The protected access modifier is accessible within package and
outside the package but through inheritance only.

The protected access modifier can be applied on the data member,


method and constructor. It can't be applied on the class.

The methods or data members declared as


protected are accessible within the same package
or subclasses in different packages.
//save by D.java
//save by C.java package mypack;
package pack; import pack.*;
public class C{
protected void msg() Class D extends C
{ {
System.out.println("Hello"); public static void main(String args[])
} {
} C obj = new C();
obj.msg();
}
}
// creating a package
package pack1;
// declaring a public class
public class A
{
// declaring method m1
public void m1()
{
System.out.println(“MCA");
}
}
12/18/24 SOBUZ 32
// importing package pack1
import pack1.A;
// driver class
class B extends A
{
// main method
public static void main(String[] args)
{
// creating an object of type class A
A a = new A();
// accessing the method m1()
a.m1();
}
}
12/18/24 33

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy