0% found this document useful (0 votes)
44 views15 pages

Oop L7

This document discusses packages and access modifiers in Java. It contains 5 sections: (1) Packages which describes what packages are and the different types, (2) Built in Packages which lists some common built-in Java packages, (3) User defined packages which explains how to create custom packages, (4) Access Modifiers which describes the different access levels (default, private, protected, public) and provides examples, and (5) Sources which lists references used to compile the content.

Uploaded by

RollingRage
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)
44 views15 pages

Oop L7

This document discusses packages and access modifiers in Java. It contains 5 sections: (1) Packages which describes what packages are and the different types, (2) Built in Packages which lists some common built-in Java packages, (3) User defined packages which explains how to create custom packages, (4) Access Modifiers which describes the different access levels (default, private, protected, public) and provides examples, and (5) Sources which lists references used to compile the content.

Uploaded by

RollingRage
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/ 15

Object Oriented

Programming

BITS Pilani
CS F213
Dubai Campus
Contents
• Packages
• Types of packages
• Examples
• Access Modifiers
• Examples

Sources:
1. Chapter 1, Cay Horstmann, Object Oriented Design & Patterns, John Wiley & Sons, 2006, 2nd Edition
2. Chapter 9, Herbert Schildt, The complete Reference Java 2, 5th Edition, Tata McGraw Hill.
3. https://www.geeksforgeeks.org/packages-in-java/
4. https://www.w3schools.com/java/java_packages.asp

BITS Pilani, Dubai Campus


Packages
• Package in Java is a mechanism to encapsulate a
group of classes, sub packages and interfaces.
• Packages are used for,
• Preventing naming conflicts. For example there can be
two classes with name Employee in two packages.
• Making searching/locating and usage of classes,
interfaces easier.
• Providing controlled access: using access specifiers.
• Packages can be considered as data encapsulation (or
data-hiding).
• Types of Packages
• Built in packages
• User defined packages

BITS Pilani, Dubai Campus


Built in Packages
• Common built in packages
• java.lang: Contains language support classes(e.g classed
which defines primitive data types, math operations). This
package is automatically imported.
• java.io: Contains classed for supporting input / output
operations.
• java.util: Contains utility classes which implement data
structures like Linked List, Dictionary and support ; for Date /
Time operations.
• java.applet: Contains classes for creating Applets.
• java.awt: Contain classes for implementing the components for
graphical user interfaces (like button , ;menus etc).
• java.net: Contain classes for supporting networking operations.
• How to add any packages in program?
=> import

BITS Pilani, Dubai Campus


User defined packages
• These are the packages that are defined by the user.
• For creating packages
• First we create a directory with name of packages.
• Then create the class inside the directory with the first
statement being the package names.
• Example 1: myPackage
• Executing created myPackage
• javac MyClass.java //Compile the class
• javac -d . MyClass.java //Compile class and package together
• javac PrintName.java //Then compile a class where package is
used
• java PrintName

BITS Pilani, Dubai Campus


Access Modifier
• Access modifiers in Java helps to restrict the scope of a
class, constructor, variable, method or data member.
•Four types of access modifiers available in java:
• Default – No keyword required
• Private
• Protected
• Public

BITS Pilani, Dubai Campus


Access Modifier
• Default
• When no access modifier is specified for a class, method, or
data member – It is said to be having the default access
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.
• Example 2:
• Here, we will create two packages (p1 and p2) and the classes
(class Try1 in p1 and class Try2 in p2) 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 the second package.
• Note: Refer to Folder p1 for p1-package and folder p2 for p2-package in course notes
of Lecture 7.

BITS Pilani, Dubai Campus


Access Modifier
Java program to illustrate error while package p2;
using class from different package import p1.*;
with default modifier class Try2
{
package p1; public static void main(String
class Try1 args[])
{ {
void display() Try1 obj = new Try1();
{ obj.display();
System.out.println("Hello }
World!"); }
} Output:
} error: package p1 does not exist import p1.*;
error: cannot find symbol
Try2 obj = new Try2();

BITS Pilani, Dubai Campus


Access Modifier
• Private
• 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 cannot be declared as private.
• Eg. Create two classes A and B within same package p1. We
will declare a method in class A as private and try to access
this method from class B and see the result.
• Example 3: Next slide

BITS Pilani, Dubai Campus


Access Modifier
Java program to illustrate error while class B
using class from different package {
with private modifier
public static void main(String args[])
package p1;
{
class A
A obj = new A();
{
private void display() //trying to access private method
of //another class
{
System.out.println(“HelloWorld!"); obj.display();
} }
} }
Output:
error: display() has private access in A

BITS Pilani, Dubai Campus


Access Modifier
• Protected
• The protected access modifier is specified using the
keyword protected.
• The methods or data members declared as protected
are accessible within same package or sub classes in different
package.
• Ex. Create two packages p1 and p2. Class A in p1 is made
public, to access it in p2. The method display in class A is
protected and class B is inherited from class A and this
protected method is then accessed by creating an object of
class B.
• Example 4: Next slides

BITS Pilani, Dubai Campus


Access Modifier
Java program to illustrate protected package p2;
modifier import p1.*;
// importing all files from package p1
package p1;
public class A
//Class B is subclass of A
{
class B extends A
protected void display()
{
{
public static void main(String args[])
System.out.println("In class A");
{
}
B obj = new B();
}
obj.display();
}
}
Output:
In class A

BITS Pilani, Dubai Campus


Access Modifier
• Public
• The public access modifier is specified using the
keyword public.
• The public access modifier has the widest scope among all
other access modifiers.
• Classes, methods or data members which are declared as
public are accessible from every where in the program. There
is no restriction on the scope of a public data members.
• Example 5: Next slides

BITS Pilani, Dubai Campus


Access Modifier
Java program to illustrate public package p2;
modifier import p1.*;
// importing all files from package p1
package p1;
public class A //Class B is subclass of A
{ class B
public void display() {
{ public static void main(String args[])
System.out.println("In class A"); {
}
A obj = new A;
}
obj.display();
}
}

Output:
In class A

BITS Pilani, Dubai Campus


BITS Pilani
Dubai Campus

Thank You!

You might also like