0% found this document useful (0 votes)
35 views26 pages

4th Module 1st Chapter

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

4th Module 1st Chapter

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

Object Oriented Programming

with JAVA
Semester 3
Course Code : BCS306A
By – Ms. Yamini Sahukar. P (Assistant Professor-AI & ML),
Bangalore Institute of Technology, Bengaluru-560004

Module: 4
(PART A)
Packages: Packages, Packages and Member Access, Importing
Packages.
Package
 Package in Java is a mechanism to encapsulate a group of classes, sub
packages and interfaces.
 Packages are used for:
1. Preventing naming conflicts.
For example there can be two classes with name Employee in two
packages,
college.staff.cse.Employee and college.staff.ee.Employee
2. Providing controlled access: protected and default have package
level access control.
 A protected member is accessible by classes in the same package and
its subclasses.
 A default member (without any access specifier) is accessible by classes
in the same package only.
3. Packages can be considered as data encapsulation (or data-hiding).
 All we need to do is put related classes into packages.
 After that, we can simply write an import class from
existing packages and use it in our program.
 A package is a container of a group of related
classes where some of the classes are accessible
are exposed and others are kept for internal
purpose.
 We can reuse existing classes from the packages as
many time as we need it in our program.
Defining a Package
 To create a package is quite easy: simply include a package command as
the first statement in a Java source file.
 Any classes declared within that file will belong to the specified package.
 The package statement defines a name space in which classes are stored.
 This is the general form of the package statement:
 package pkg;
 pkg is the name of the package
 Example: package mypackage;
 Java uses file system directories to store packages.

 For example, the .class files for any classes you declare to be part of
mypackage must be stored in a directory called mypackage.

 The case is significant, and the directory (A file system structure containing
files and other directories is called directories) name must match the package
name exactly.

 More than one file can include the same package statement. The package
statement simply specifies to which package the classes defined in a file
belong. It does not exclude other classes in other files from being part of that
same package.

 You can create a hierarchy of packages. To do so, simply separate each


package name from the one above it by use of a period. The general form of a
multileveled package statement is shown here:

 package pkg1[.pkg2[.pkg3]];

 Example: package a.b.c;


 Finding Packages and CLASSPATH

 How does the Java run-time system know where to look for packages
that you create?
 First, by default, the Java run-time system uses the current working
directory as its starting point. Thus, if your package is in a subdirectory
of the current directory, it will be found.
 Second, you can specify a directory path or paths by setting the
CLASSPATH environmental variable.
 Third, you can use the -classpath option with java and javac to specify
the path to your classes
 Example: package mypack;

 In order for a program to find mypack, the program can be executed from
a directory immediately above mypack, or the CLASSPATH must be set to
include the path to mypack, or the -classpath option must specify the
path to mypack when the program is run via java. When the second two
options are used, the class path must not include mypack, itself. It must
simply specify the path to mypack. For example, in a Windows
environment, if the path to mypack is

 C:\MyPrograms\Java\mypack

 then the class path to mypack is

 C:\MyPrograms\Java
 How packages work?
 Package names and directory structure are closely
related.
 For example if a package name is college.staff.cse,
then there are three
directories, college, staff and cse such that cse is
present in staff and staff is present
inside college.
 Also, the directory college is accessible
through CLASSPATH variable, i.e., path of parent
directory of college is present in CLASSPATH. The
idea is to make sure that classes are easy to locate.
A Short Package Example
Cont…

Call this file AccountBalance.java and put it in a directory called mypack. Next, compile
the file. Make sure that the resulting .class file is also in the mypack directory. Then, try
executing the AccountBalance class, using the following command line:
java mypack.AccountBalance
Cont…
 Remember, you will need to be in the directory above mypack
when you execute this command. (Alternatively, you can use one
of the other two options described in the preceding section to
specify the path mypack.) As explained, AccountBalance is now
part of the package mypack. This means that it cannot be
executed by itself. That is, you cannot use this command line:

 java AccountBalance

 AccountBalance must be qualified with its package name


Package naming conventions :
 Packages are named in reverse order of domain
names, i.e., org.geeksforgeeks.practice.
 For example, in a college, the recommended
convention is-
rnsit.btech.aiml,
college.tech.cse,
college.tech.ee,
college.art.history, etc.
 Adding a class to a Package : We can add more classes to a
created package by using package name at the top of the program
and saving it in the package directory.
 We need a new java file to define a public class, otherwise we can
add the new class to an existing .java file and recompile it.
 Subpackages: Packages that are inside another package are the
subpackages.
 These are not imported by default, they have to imported explicitly.
 Also, members of a subpackage have no access privileges, i.e., they
are considered as different package for protected and default access
specifiers.
 Example :
import java.util.*;
 util is a subpackage created inside java package.
 Classes and packages are both means of encapsulating and containing the
name space and scope of variables and methods.
 Packages act as containers for classes and other subordinate packages.
Classes act as containers for data and code.
 The class is Java’s smallest unit of abstraction.
 The three access modifiers, private, public, and protected, provide a variety
of ways to produce the many levels of access required by these categories.
Packages and Member Access
Member Access: Java provides four distinct access
levels for member variables and methods: private,
protected, public, and, if left unspecified, package-
access.
1. private: The access level of a private modifier is
only within the class. It cannot be accessed from
outside the class.
2. default (no explicit modifier): The access level of a
default modifier is only within the package. It
cannot be accessed from outside the package.
3. protected: The access level of a protected modifier is
within the package and outside the package through a
child class. If you do not make the child class, it cannot
be accessed from outside the package.
4. public: The access level of a public modifier is
everywhere. It can be accessed from within the class,
outside the class, within the package, and outside the
package.
SCOPE:
 These access modifiers help to restrict the scope of a
class, constructor, variable, method, or data member.
 They provide security and accessibility to the user
depending upon the access modifier used with the
element
Importing Packages
 In a Java source file, import statements occur immediately
following the package statement (if it exists) and before any class
definitions.
 This is the general form of the import statement:

 Here, pkg1 is the name of a top-level package, and pkg2 is the


name of a subordinate package inside the outer package separated
by a dot (.).
 There is no practical limit on the depth of a package hierarchy,
except that imposed by the file system.
 Finally, you specify either an explicit classname or a star (*), which
indicates that the Java compiler should import the entire package.
Importing Packages
 The basic language functions are stored in a package
called java.lang.
 Normally, you have to import every package or class
that you want to use, but since Java is useless
without much of the functionality in java.lang, it is
implicitly imported by the compiler for all programs.
 This is equivalent to the following line being at the
top of all of your programs: import java.lang.* ;
 It must be emphasized that the import statement is
optional.
 Any place you use a class name, you can use its fully
qualified name, which includes its full package hierarchy.
 For example, this fragment uses an import statement:
import java.util.*;
class MyDate extends Date {
}
The same example without the import statement looks like
this:
class MyDate extends java.util.Date {
}
 In Java, packages are used to group related classes and
interfaces together.
 Importing a package or a class in Java allows you to use
its classes and interfaces in your code.
 Here’s how you can import packages in Java:
 1. Import a Specific Class from a Package:
import package.name.ClassName;
This statement imports a specific class from a package.
 2. Import All Classes from a Package:
import package.name.*;
This statement imports all the classes from a package
As for commonly used import statements in Java, here are a few examples:
 1. java.util package: This package contains the collections
framework, legacy collection classes, event model, date and
time facilities, internationalization, and miscellaneous utility
classes (a string tokenizer, a random-number generator, and a
bit array).
import java.util.*;
2.java.io package: This package provides for system input and
output through data streams, serialization and the file system.
import java.io.*;
3. java.lang package: This package provides classes that are
fundamental to the design of the Java programming
language. The java.lang package is automatically imported
into every Java program.
4. java.net package: This package provides the classes for
implementing networking applications.
import java.net.*;
5. java.sql package: This package provides the API for
accessing and processing data stored in a data source
(usually a relational database) using the Java programming
language.
import java.sql.*;
Program : To demonstrate the
use of private, public, and
protected access modifiers.

// The private field can


only be accessed within
its own class, the public
field can be accessed
from anywhere, and the
protected field can be
accessed within its own
class and by subclasses.
Built-in package and a User-defined package
 In Java, packages are used to group related classes, interfaces,
and sub-packages.
 They help in avoiding naming conflicts and make the code easier to
manage.
 Packages in Java are divided into two types:
 1. Built-in Packages: These are the packages that come with the
Java Development Kit (JDK).
 They are also known as standard or built-in libraries.
 Examples of built-in packages include java.lang, java.util, java.io,
java.net, java.sql, and many others.
 These packages contain a large number of classes and interfaces
that are commonly used in Java programming.
2.User-defined Packages:
 These are the packages that are defined by the user or programmer.
 User-defined packages help in organizing your own classes and
interfaces so that they can be reused in other programs.
 When you create a class in Java, you can declare it as part of a
package.
An example of how to create a user-defined package in Java:
 In this example, a package named mypackage is created,
and a class named MyClass is defined inside this package.
 The display method in MyClass prints a message when
called.
 To use this class in another program, you would import it
like this:

You might also like