Packages (28 03 25)
Packages (28 03 25)
Package in java can be categorized in two form, built-in package, and user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
Here, we will have the detailed learning of creating and using user-defined packages.
1) Java package is used to categorize the classes and interfaces so that they can be easily
maintained.
Built-in Packages:
The built-in packages are the packages from java API. The Java API is a pre-defined classes,
interfaces, and sub-packages library. The built-in packages were included in the JDK.There
are many built-in packages in java, few of them are as java, lang, io, util, awt, javax, swing,
net, sql, etc. We need to import the built-in packages to use them in our program. To import a
package, we use the import statement. For e.g: we have used java.io package previously
which contain classes to support input / output operations in Java. Similarly, there are other
packages which provides different functionality.
or
2) import java.util.*; // import all the class from util package
First statement imports Vector class from util package which is contained inside java
package.
Second statement imports all the classes from util package.
User-defined Packages:
The user-defined packages are the packages created by the user. User is free to create
their own packages.
package packageName;
1. //save as Simple1.java
2. package mypack;
3. public class Simple1{
4. public static void main(String args[]){
5. System.out.println("Welcome to package");
6. }
7. }
The -d switch specifies the destination where to put the generated class file. You can use any
directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to
keep the package within the same directory, you can use . (dot).
You need to use fully qualified name e.g. mypack.Simple1 etc to run the class.
To Compile: javac -d . Simple1.java
There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
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 of package that import the packagename.*
1. //save by A.java
2. package pack;
3. public class A{
4. public void msg(){System.out.println("Hello");}
5. }
1. //save by B.java
2. import pack.*;
3. class B{
4. public static void main(String args[]){
5. A obj = new A();
6. obj.msg();
7. }
8. }
Output:
Hello
2) Using packagename.classname
In java, the import keyword used to import built-in and user-defined packages. When a
package has imported, we can refer to all the classes of that package using their name
directly.
The import statement must be after the package statement, and before any other statement.
Using an import statement, we may import a specific class or all the classes from a package.
🔔 Using one import statement, we may import only one package or a class.
🔔 Using an import statement, we can not import a class directly, but it must be a part of a
package.
🔔 A program may contain any number of import statements.
Importing specific class
Using an importing statement, we can import a specific class. The following syntax is
employed to import a specific class.
Syntax
import packageName.ClassName;
If you import package.classname then only declared class of this package will be accessible.
1. //save by A.java
2.
3. package pack;
4. public class A{
5. public void msg(){System.out.println("Hello");}
6. }
1. //save by B.java
2. import pack.A;
3.
4. class B{
5. public static void main(String args[]){
6. A obj = new A();
7. obj.msg();
8. }
9. }
Output:
Hello
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
class B{
public static void main(String args[]){
A obj = new A(); //using fully qualified name
obj.msg();
}
}
Output:
Hello
Explicit import: Classes are available inside the package, Explicit means direct or when we
give the proper path of the java class it will call as explicit import.e.g: import
java.util.ArrayList;
Explicit import
Implicit import : Implicit means indirect, When we load all the classes of the package in our
java code by using (*) it will call as implicit import. E.g: import java.util.*;
Here we have loaded all the classes from util package.
UNDERSTANDING CLASSPATH
What is ClassPath?
From the word, we understand that classpath is a path of a class. A classpath is a path for the
compiler to identify which class, or which package we use in our defined class. It provides
the compiler a way to find classes or packages that are used in our class.
CLASSPATH describes the location where all the required files are available which are
used in the application. Java Compiler and JVM (Java Virtual Machine) use
CLASSPATH to locate the required files. If the CLASSPATH is not set, Java Compiler
will not be able to find the required files and hence will throw the following error.
Error: Could not find or load main class <class name> (e.g. GFG)
The above error is resolved when CLASSPATH is set.
Command Prompt:
set PATH=.;C:\Program Files\Java\JDK1.6.20\bin
Note: Semi-colon (;) is used as a separator and dot (.) is the default value
of CLASSPATH in the above command.
1. Select Start
1. Select OK.
The keyword used to create a class is The keyword used to create an interface is
“class” “interface”
An interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract
methods in the Java interface, not method body. It is used to achieve abstraction and
multiple inheritance in Java. In other words, you can say that interfaces can have abstract
methods and variables. It cannot have a method body. Java Interface also represents the IS-
A relationship.
There are mainly three reasons to use interface. They are given below.
o It is used to achieve abstraction.
o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling.
An interface is declared by using the interface keyword. It provides total abstraction; means
all the methods in an interface are declared with the empty body, and all the fields are public,
static and final by default. A class that implements an interface must implement all the
methods declared in the interface.
Syntax:
interface <interface_name>
{
// declare constant fields
// declare methods that abstract
// by default.
}
Implementing Interfaces
Once an interface has been defined, one or more classes can inherit that interface by
using implements Keyword
The methods that implement an interface in a class must be declared with public
access Specifier.
Syntax:
class classname [extends superclass] [implements interface [,interface...]]
{
// class-body
}
If a class implements more than one interface, the interfaces are separated with a comma.
As shown in the figure given below, a class extends another class, an interface extends
another interface, but a class implements an interface.
Applying interfaces
In this example, the Printable interface has only one method, and its implementation is
provided in the A6 class.
1. interface printable{
2. void print();
3. }
4. class A6 implements printable{
5. public void print(){System.out.println("Hello");}
6.
7. public static void main(String args[]){
8. A6 obj = new A6();
9. obj.print();
10. }
11. }
Output:
Hello
As we have explained in the inheritance , multiple inheritance is not supported in the case
of class because of ambiguity. However, it is supported in case of an interface because there
is no ambiguity. It is because its implementation is provided by the implementation class.
.Interface inheritance
Output:
Hello
Welcome
An interface, i.e., declared within another interface or class, is known as a nested interface.
The nested interfaces are used to group related interfaces so that they can be easy to maintain.
The nested interface must be referred to by the outer interface or class. It can't be accessed
directly.
There are given some points that should be remembered by the java programmer.
o The nested interface must be public if it is declared inside the interface, but it can
have any access modifier if declared within the class.
o Nested interfaces are declared static.
In this example, we will learn how to declare the nested interface and how we can access it.
TestNestedInterface1.java
interface Showable
{
interface Message{
void msg();
}
}
class TestNestedInterface1 implements Showable.Message{
public void msg(){System.out.println("Hello nested interface");}
public static void main(String args[]){
Showable.Message message=new TestNestedInterface1();//upcasting here
message.msg();
}
}
Output:
Let's see how we can define an interface inside the class and how we can access it.
TestNestedInterface2.java
1. class A
2. {
3. interface Message
4. {
5. void msg();
6. }
7. }
8. class TestNestedInterface2 implements A.Message
9. {
10. public void msg(){System.out.println("Hello nested interface");
11. }
12. public static void main(String args[])
13. {
14. A.Message message=new TestNestedInterface2();//upcasting here
15. message.msg();
16. }
17. }
Output:
interface SampleInterface{
}
Extending an Interface in java
In java, an interface can extend another interface. When an interface wants to extend another
interface, it uses the keyword extends. The interface that extends another interface has its
own members and all the members defined in its parent interface too. The class which
implements a child interface needs to provide code for the methods defined in both child and
parent interfaces, otherwise, it needs to be defined as abstract class.
🔔 An interface can extend another interface.
🔔 An interface can not extend multiple interfaces.
🔔 An interface can implement neither an interface nor a class.
🔔 The class that implements child interface needs to provide code for all the methods
defined in both child and parent interfaces.
1. interface Printable{
2. void print();
3. }
4. interface Showable extends Printable{
5. void show();
6. }
7. class TestInterface4 implements Showable{
8. public void print(){System.out.println("Hello");}
9. public void show(){System.out.println("Welcome");}
10.
11. public static void main(String args[]){
12. TestInterface4 obj = new TestInterface4();
13. obj.print();
14. obj.show();
15. }
16. }
Output:
DIFFERENCE BETWEEN CLASSES AND INTERFACES
Class Interface
The keyword used to create a class is The keyword used to create an interface is
“class” “interface”
An interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract
methods in the Java interface, not method body. It is used to achieve abstraction and
multiple inheritance in Java. In other words, you can say that interfaces can have abstract
methods and variables. It cannot have a method body. Java Interface also represents the IS-
A relationship.
There are mainly three reasons to use interface. They are given below.
o It is used to achieve abstraction.
o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling.
An interface is declared by using the interface keyword. It provides total abstraction; means
all the methods in an interface are declared with the empty body, and all the fields are public,
static and final by default. A class that implements an interface must implement all the
methods declared in the interface.
Syntax:
interface <interface_name>
{
// declare constant fields
// declare methods that abstract
// by default.
}
Implementing Interfaces
Once an interface has been defined, one or more classes can inherit that interface by
using implements Keyword
The methods that implement an interface in a class must be declared with public
access Specifier.
Syntax:
class classname [extends superclass] [implements interface [,interface...]]
{
// class-body
}
If a class implements more than one interface, the interfaces are separated with a comma.
As shown in the figure given below, a class extends another class, an interface extends
another interface, but a class implements an interface.
Applying interfaces
In this example, the Printable interface has only one method, and its implementation is
provided in the A6 class.
6. interface printable{
7. void print();
8. }
9. class A6 implements printable{
10. public void print(){System.out.println("Hello");}
6.
12. public static void main(String args[]){
13. A6 obj = new A6();
14. obj.print();
15. }
16. }
Output:
Hello
As we have explained in the inheritance , multiple inheritance is not supported in the case
of class because of ambiguity. However, it is supported in case of an interface because there
is no ambiguity. It is because its implementation is provided by the implementation class.
.Interface inheritance
Output:
Hello
Welcome
An interface, i.e., declared within another interface or class, is known as a nested interface.
The nested interfaces are used to group related interfaces so that they can be easy to maintain.
The nested interface must be referred to by the outer interface or class. It can't be accessed
directly.
There are given some points that should be remembered by the java programmer.
o The nested interface must be public if it is declared inside the interface, but it can
have any access modifier if declared within the class.
o Nested interfaces are declared static.
In this example, we will learn how to declare the nested interface and how we can access it.
TestNestedInterface1.java
interface Showable
{
interface Message{
void msg();
}
}
class TestNestedInterface1 implements Showable.Message{
public void msg(){System.out.println("Hello nested interface");}
public static void main(String args[]){
Showable.Message message=new TestNestedInterface1();//upcasting here
message.msg();
}
}
Output:
Let's see how we can define an interface inside the class and how we can access it.
TestNestedInterface2.java
4. class A
5. {
6. interface Message
4. {
7. void msg();
8. }
7. }
8. class TestNestedInterface2 implements A.Message
9. {
13. public void msg(){System.out.println("Hello nested interface");
14. }
15. public static void main(String args[])
13. {
18. A.Message message=new TestNestedInterface2();//upcasting here
19. message.msg();
20. }
21. }
Output:
interface SampleInterface{
}
Extending an Interface in java
In java, an interface can extend another interface. When an interface wants to extend another
interface, it uses the keyword extends. The interface that extends another interface has its
own members and all the members defined in its parent interface too. The class which
implements a child interface needs to provide code for the methods defined in both child and
parent interfaces, otherwise, it needs to be defined as abstract class.
🔔 An interface can extend another interface.
🔔 An interface can not extend multiple interfaces.
🔔 An interface can implement neither an interface nor a class.
🔔 The class that implements child interface needs to provide code for all the methods
defined in both child and parent interfaces.
Output:
DIFFERENCE BETWEEN CLASSES AND INTERFACES
Class Interface
The keyword used to create a class is The keyword used to create an interface is
“class” “interface”
An interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract
methods in the Java interface, not method body. It is used to achieve abstraction and
multiple inheritance in Java. In other words, you can say that interfaces can have abstract
methods and variables. It cannot have a method body. Java Interface also represents the IS-
A relationship.
There are mainly three reasons to use interface. They are given below.
o It is used to achieve abstraction.
o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling.
An interface is declared by using the interface keyword. It provides total abstraction; means
all the methods in an interface are declared with the empty body, and all the fields are public,
static and final by default. A class that implements an interface must implement all the
methods declared in the interface.
Syntax:
interface <interface_name>
{
// declare constant fields
// declare methods that abstract
// by default.
}
Implementing Interfaces
Once an interface has been defined, one or more classes can inherit that interface by
using implements Keyword
The methods that implement an interface in a class must be declared with public
access Specifier.
Syntax:
class classname [extends superclass] [implements interface [,interface...]]
{
// class-body
}
If a class implements more than one interface, the interfaces are separated with a comma.
As shown in the figure given below, a class extends another class, an interface extends
another interface, but a class implements an interface.
Applying interfaces
In this example, the Printable interface has only one method, and its implementation is
provided in the A6 class.
Output:
Hello
As we have explained in the inheritance , multiple inheritance is not supported in the case
of class because of ambiguity. However, it is supported in case of an interface because there
is no ambiguity. It is because its implementation is provided by the implementation class.
.Interface inheritance
Output:
Hello
Welcome
An interface, i.e., declared within another interface or class, is known as a nested interface.
The nested interfaces are used to group related interfaces so that they can be easy to maintain.
The nested interface must be referred to by the outer interface or class. It can't be accessed
directly.
There are given some points that should be remembered by the java programmer.
o The nested interface must be public if it is declared inside the interface, but it can
have any access modifier if declared within the class.
o Nested interfaces are declared static.
In this example, we will learn how to declare the nested interface and how we can access it.
TestNestedInterface1.java
interface Showable
{
interface Message{
void msg();
}
}
class TestNestedInterface1 implements Showable.Message{
public void msg(){System.out.println("Hello nested interface");}
public static void main(String args[]){
Showable.Message message=new TestNestedInterface1();//upcasting here
message.msg();
}
}
Output:
Let's see how we can define an interface inside the class and how we can access it.
TestNestedInterface2.java
7. class A
8. {
9. interface Message
4. {
9. void msg();
10. }
7. }
8. class TestNestedInterface2 implements A.Message
9. {
16. public void msg(){System.out.println("Hello nested interface");
17. }
18. public static void main(String args[])
13. {
22. A.Message message=new TestNestedInterface2();//upcasting here
23. message.msg();
24. }
25. }
Output:
interface SampleInterface{
}
Extending an Interface in java
In java, an interface can extend another interface. When an interface wants to extend another
interface, it uses the keyword extends. The interface that extends another interface has its
own members and all the members defined in its parent interface too. The class which
implements a child interface needs to provide code for the methods defined in both child and
parent interfaces, otherwise, it needs to be defined as abstract class.
🔔 An interface can extend another interface.
🔔 An interface can not extend multiple interfaces.
🔔 An interface can implement neither an interface nor a class.
🔔 The class that implements child interface needs to provide code for all the methods
defined in both child and parent interfaces.
Output: