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

Java Packages

Packages in Java allow for better organization and avoidance of name conflicts. There are built-in packages provided by Java as well as user-defined packages that can be created. Packages group related classes and interfaces, provide access protection and namespace management. This allows projects to be organized and classes to be reused across packages.

Uploaded by

Treymax Sikan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Java Packages

Packages in Java allow for better organization and avoidance of name conflicts. There are built-in packages provided by Java as well as user-defined packages that can be created. Packages group related classes and interfaces, provide access protection and namespace management. This allows projects to be organized and classes to be reused across packages.

Uploaded by

Treymax Sikan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

PRESENTATION ON JAVA PACKAGES

BY BETTY PADAMBO BANDA


PACKAGES IN JAVA

• INTRODUCTION
• A Package can be defined as a grouping of related types of classes, interfaces
and subpackages, providing access protection and namespace management, In
other words a package as the name suggests is a pack (group) of classes and
other packages. In java we use packages to organize our classes and interfaces.
Since the package creates a new namespace there won't be any name conflicts
with names in other packages. Using packages, it is easier to provide access
control and it is also easier to locate the related classes.
• In Java While creating a package, you should choose a name for the package
and include a package statement along with that name at the top of every
source file that contains the classes, interfaces and subpackages.
• Syntax
• package.package_name;
Types of Packages

• They are different types of packages in Java namely built-in packages and the
packages we can create (also known as user defined package).
• Built-in Packages
• The already defined package are also known as built-in packages. There are many
built-in packages such as java, Lang, awt, javax, swing, net, io, util, sql.
• In java we have several built-in packages, for example when we need user input, we
import a package like this:
• import java.util.Scanner
• Here:
• → Java is a top level package
• → util is a sub package
• → And Scanner is a class which is present in the sub package util.
Types of Built-In Packages

• The two common built-in packages are Java.io and java. Lang.
• Java.io
• In Java.io classes for input , output functions are bundled in a package.
• Example of Java.io
• Package com.padambo;
• import java.io.Kasam;
• public class Kasambala {
• public static void main(String[] args) {
• kasam kas = null;
• String name = null;
• try {
// creates a kasam object
kas = System.kasam();
// if kasam is not null
if (kas != null) {
// read line from the user input
name = kas.readLine("Name: ");
// prints
System.out.println("Name entered : " + name);
}
} catch(Exception ex) {
// if any error occurs
ex.printStackTrace();
}
}
}
After running the program it will produce the following result −
Name: Master Programmer
Name entered : Master Programmer
Java.Lang
java.lang these are built-in packages that bundles the fundamental
classes.
Example of java.lang
package com.padambo;
Import java.lang.;
class A {
Public static void show() {
System.out.println ("Class A show () function");
}
}
class B extends A {
public static void show() {
System.out.println ("Class B show() function");
}
}
public class ClassDemo {
public static void main(String[] args) {
ClassDemo cls = new ClassDemo();
Class c = cls.getClass();
System.out.println(c);
Object obj = new A();
B b1 = new B();
b1.show();
// casts object
Object a = A.class.cast(b1);
System.out.println(obj.getClass());
System.out.println (b1.getClass());
System.out.println(a.getClass());
}
}
After running the above program, it will produce the following result −
class com.padambo.ClassDemo
Class B show () function
class com.padambo.A
class com.padambo
class com.padambo.B
User Defined Packages

• The package we create is called user-defined.


• Example of User Defined
• I have created a class Calculator inside a package name letmecalculate. To
create a class inside a package, declare the package name in the first
statement in your program. A class can have only one package declaration.
• Calculator.java file created inside a package letmecalculate
• package letmecalculate;
• public class Calculator {
• public int add(int a, int b){
• return a+b;
• }
public static void main(String args[]){
Calculator obj = new Calculator();
System.out.println(obj.add(10, 20));
}
Sub packages in Java

• A package inside another package is known as sub package.


• For example If you create a package inside letmecalculate package then that
will be called sub package.
• package letmecalculate.multiply;
• Multiplication.java
• package letmecalculate.multiply;
• public class Multiplication {
• int product(int a, int b){
• return a*b;
• }
• }
if you need to use this Multiplication class you have to either import the
package like this: import letmecalculate.multiply;
Advantages of using a package in Java

• • Reusability: When developing a project in java we can avoid


writing the same code or same things over and over by Using packages,
you can create such things in form of classes inside a package and
whenever you need to perform that same task, just import that package
and use the class.
• • Better Organization: In large java projects where we have several
hundreds of classes, it is always required to group the similar types of
classes in a meaningful package name so that you can organize your
project better and when you need something you can quickly locate it
and use it.
• • Name Conflicts: We can define two classes with the same name in
different packages so to avoid name collision, we can use packages.
CONCLUSION

• As a developer you should know you will get to update the codes
at some point and there will be bugs as well.There will be changes
as per the client on many occasions.This is a reason Java package
are used to categorize the classes and interfaces so that they can
be easily maintained which helps remove naming collision. We
can also create our own Package or extend already available
Package.
REFERENCES

• 1. Oracle Built-in Packages by John Beresniewicz, Charles Dye,


Steven Feuerstein
• 2.Head First Java, 2nd Edition
• 3’.Thinking in Java (4th Edition)
• 4.Introduction to Java by Sedgewick
• 5.Java in a Nutshell
• 6.Core Java Volume I--Fundamentals (9th Edition) (Core Series):
Cay S. Horstmann
• 7.Java How To Program (late objects) by Paul Deitel, Harvey Deitel
END OF PRESENTATION

You might also like