Java Packages
Java Packages
• 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
• 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