Lec_12
Lec_12
Packages
• A package in Java is used to group related classes.
Think of it as a folder in a file directory. We use
packages to avoid name conflicts, and to write a
better maintainable code. Packages are divided
into two categories:
• Built-in Packages (packages from the Java API)
• User-defined Packages (create your own packages)
• There are many built-in packages such as java,
lang, awt, javax, swing, net, io, util, sql etc.
• Advantage of Java Package
• 1) Java package is used to categorize the classes and interfaces so that they
can be easily maintained.
• 2) Java package provides access protection.
• 3) Java package removes naming collision.
• 1. Built-in Packages
• These packages consist of a large number of classes which are a part of
Java API.Some of the commonly used built-in packages are:
• java.lang: Contains language support classes(e.g classes which defines
primitive data types, math operations). This package is automatically
imported.
• java.io: Contains classes for supporting input / output operations.
• java.util: Contains utility classes which implement data structures like Linked
List, Dictionary and support ; for Date / Time operations.
• 2. User-defined Packages
• These are the packages that are defined by the user.
• To create your own package, you need to understand
that Java uses a file system directory to store them. Just
like folders on your computer:
• To create a package, use the package keyword:
package mypack;
class MyPackageClass {
public static void main(String[] args) {
System.out.println("This is my package!");
}
}
• Save the file as MyPackageClass.java, and compile it:
• javac -d . MyPackageClass.java
• 1) Using packagename.*
• If you use package.* then all the classes and interfaces
of this package will be accessible but not subpackages.
• The import keyword is used to make the classes and
interface of another package accessible to the current
package.
• Example –
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
• Now first compile A.java –
• javac -d . A.java
• Compile B.java –
• javac -d . B.java
• Now run the B class file –
• java mypack.B
• 2) Using packagename.classname
• If you import package.classname then only
declared class of this package will be
accessible.
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
• 3) Using fully qualified name
• If you use fully qualified name then only
declared class of this package will be
accessible. Now there is no need to import.
But you need to use fully qualified name every
time when you are accessing the class or
interface.
• It is generally used when two packages have
same class name e.g. java.util and java.sql
packages contain Date class.
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();//using fully qualified name
obj.msg();
}
}
• If you import a package, all the classes and interface of that package will
be imported excluding the classes and interfaces of the subpackages.
Hence, you need to import the subpackage as well.
• Subpackage in java
• Package inside the package is called the subpackage. It should
be created to categorize the package further.
• Let's take an example, Sun Microsystem has definded a
package named java that contains many classes like System,
String, Reader, Writer, Socket etc. These classes represent a
particular group e.g. Reader and Writer classes are for
Input/Output operation, Socket and ServerSocket classes are
for networking etc and so on. So, Sun has subcategorized the
java package into subpackages such as lang, net, io etc. and
put the Input/Output related classes in io package, Server and
ServerSocket classes in net packages and so on.
• The standard of defining package is domain.company.package
e.g. com.chitkara.bean or org.sssit.dao.
• Example –
package com.chitkara.core;
class Demo{
public static void main(String args[]){
System.out.println("Hello subpackage");
}
}
• Compile it - java com.chitkara.core.Demo
• Run it - java com.chitkara.core.Demo
• Static Import in Java –
• In Java, static import (import static) allows us to import static members
(methods and variables) of a class directly, so we don't need to prefix
them with the class name.
// Define an interface
interface Animal {
void makeSound(); // Abstract method (public & abstract by default)
}
interface Vehicle {
void start();
}
• Unlike classes, Java does not support multiple inheritance with classes but allows multiple inheritance
with interfaces.
• Example –
interface Printable {
void print();
}
interface Showable {
void show();
}
interface Constants {
int MAX_VALUE = 100; // Implicitly public, static, and final
}
interface Vehicle {
void start();
}
interface Printer {
void print();
}
// If we want a new feature like duplex printing, we must implement it in each class.
• Each class must implement duplicate code separately.
• With Java 8 Default Methods (Code Reusability) –
interface Printer {
void print();