MSharmilaDeviITDEPT
449 views
26 slides
Dec 06, 2021
Slide 1 of 26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
About This Presentation
Packages:Putting Classes Together
Size: 715.37 KB
Language: en
Added: Dec 06, 2021
Slides: 26 pages
Slide Content
PACKAGES By, M.SHARMILA DEVI M.Sc (IT)
Introduction The main features of OOP is its ability to reuse the code already created, One way of achieving this is by the use of packages Packages are Java’s way of grouping a variety of classes or interfaces together. Packages act as “containers” for classes.
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.
Java API Packages Java API provides a large number of classes grouped into different packages according to functionality Most of the time we use the packages available with the Java API.
Functional breakdown of packages
Java System Packages and Their Classes
Using System Packages Hierarchical representation of java.awt package
The packages are organised in a hierarchical structure. It show package name java contains the package awt, which in turn contains various classes required for implementing graphical user interface. There are two ways of accessing the classes stored in a package. Fully qualified class name Import statements
The first approach is to use the fully qualified class name of the class we want to use. java.awt.Colour Awt is a package within the package java and the hierarchy is represented by separating the levels with dots. Import packagename.classname; Or Import packagename.*; These are known as import statements and must appear at the top of the file before any class declarations.
Import java.awt.Color; This statement allows the specified class in the specified package to be imported. The above statement imports the class Colour and therefore the class name can now be directly used in the program. Import java.awt.*; This statement imports every class contained in the specified package. It will bring all classes of java.awt package.
Naming Conventions Packages can be named using the standard Java naming rules. Packages should begin with lowercase letters by convention. It distinguish package names from class names.
Creating Packages We must first declare the name of the package using the package keyword followed by a package name. This must be the first statement in a Java source file. Example
Accessing a Package Java system package can be accessed Using a fully qualified class name. Or Using a shortcut approach through the import statement. The same approaches can be used to access the user defined packages as well.
The general form of import statement for searching a class is as follows: Package 1 is the name of the top level package, package2 is the name of the package that is inside the package1, and so on. Finally, the explicit classname is specified. Another approach as follows: Import packagename.*; The star(*) indicates that the compiler should search entire package hierarchy when it encounters a class name.
Using a Package It shows a package named package1 containing a single ClassA
This program that imports the class ClassA from the package package1.
Let us consider another package named package2 containing again a single class.
In this program, it uses classes contained in both the packages. So it imports package1 and package2.
The output will be Class A Class B m = 10
Subclassing an imported class
It is also possible to subclass a class that has been imported from another package. The output will be Class B m = 10 Class C m = 10 n = 20
Access Protection
Adding a Class to a Package It is simple to add a class to an existing package.
The package p1 contains one public class by name A. If we want to add another class B to this package. This can be done as follows: Define the class and make it public. Place the package statement package p1; before the class definition as follows: package p1; public class B { //body of B }
3. Store this as B.java file under the directory p1. 4. Compile B.java file. This will create a B.class file and place it in the directory p1. To create a package with multiple public classes, we may follow the following steps: 1.Decide the name of the package 2. Create a subdirectory with this name under the directory where main source files are stored.
3. Create classes that are to be placed in the package in separate source files and declare the package statement package packagename; at the top of each source file 4. Switch to the subdirectory created earlier and compile each source file. when completed, the package would contain .class files of all the source files.