Packages and Interfaces
Packages and Interfaces
Packages and Interfaces
The already defined package like java.io.*, java. lang.* etc., are known as built-
in packages. There are many built-in packages such as java, lang, awt, javax,
swing, net, io, util, sql etc.
package mypack;
public class Simple
{
public static void main(String args[])
{
System.out.println("Welcome to package");
}
}
Steps to create User-defined Packages
Step 1: Creating a package in java class. Just write a package by following its
name.
package example1;
Step 2: Include class in java package, but remember that class only have one package
declaration.
package example1;
class gfg
{
public static void main(Strings[] args)
{
-------function--------
}
}
Step 3: Now the user-defined package is successfully created, we can import it into other
packages and use functions
In this example, we’ll create a user-defined package and function to print a message for the
users.
package example;
import example.gfg;
There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
Using packagename.*
If you use package.* then all the classes and interfaces of this package
will be accessible but not subpackages.
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Using packagename.classname
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();
}
}
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();
}
}
Illustration of user-defined packages:
Creating first package:
package package_name; Making use of both the created packages:
import package_one.ClassTwo;
public class ClassOne {
import package_name.ClassOne;
public void methodClassOne() {
System.out.println("Hello there its
public class Testing {
ClassOne");
public static void main(String[] args){
}
ClassTwo a = new ClassTwo();
}
ClassOne b = new ClassOne();
Creating second package:
a.methodClassTwo();
b.methodClassOne();
package package_one;
}
}
public class ClassTwo {
public void methodClassTwo(){
System.out.println("Hello there i am
ClassTwo");
}
}
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.
class A{
private int data=40;
private void msg(){System.out.println("Hello java");}
}
The protected access modifier is accessible within package and outside the package but
through inheritance only.
The protected access modifier can be applied on the data member, method and constructor.
It can't be applied on the class.
It provides more accessibility than the default modifer.
//save by A.java
package pack;
public class A{
protected void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}
Public
The public access modifier is accessible everywhere. It has the widest scope
among all other modifiers.
//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();
}
}
Interface in Java
An interface in Java is a blueprint of a class. It has static constants and abstract
methods.
In other words, you can say that interfaces can have abstract methods and
variables. It cannot have a method body.
An interface is a description of the actions that an object can do... for example
when you flip a light switch, the light goes on, you don't care how, just that it
does.
Using the keyword interface, you can fully abstract a class’ interface from its
implementation.
That is, using interface, you can specify what a class must do, but not how it
does it.
Interfaces are syntactically similar to classes, but they lack instance variables,
and their methods are declared without any body.
In practice, this means that you can define interfaces that don’t make
assumptions about how they are implemented. Once it is defined, any number
of classes can implement an interface. Also, one class can implement any
number of interfaces.
To implement an interface, a class must create the complete set of methods
defined by the interface. However, each class is free to determine the details
of its own implementation.
By providing the interface keyword, Java allows you to fully utilize the “one
interface, multiple methods” aspect of polymorphism.
Why use Java interface?
• It can be used to achieve loose coupling. Loose coupling in Java means that the
classes are independent of each other.
How to declare an interface?
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:
access interface name {
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
// ...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}
When no access specifier is included, then default access results, and the
interface is only available to other members of the package in which it is
declared.
When it is declared as public, the interface can be used by any other code.
In this case, the interface must be the only public interface declared in the
file, and the file must have the same name as the interface.
name is the name of the interface, and can be any valid identifier. Notice
that the methods that are declared have no bodies. They end with a
semicolon after the parameter list. They are, essentially, abstract methods;
there can be no default implementation of any method specified within an
interface. Each class that includes an interface must implement all of the
methods.
Variables can be declared inside of interface declarations. They are
implicitly final and static, meaning they cannot be changed by the
implementing class. They must also be initialized.
interface Callback {
void callback(int param);
}
Implementing Interfaces
Once an interface has been defined, one or more classes can implement that
interface. To implement an interface, include the implements clause in a class
definition, and then create the methods defined by the interface. The general
form of a class that includes the implements clause looks like this:
class classname [extends superclass] [implements interface [,interface...]]
{
// class-body
}
If a class implements more than one interface, the interfaces are separated
with a comma. If a class implements two interfaces that declare the same
method, then the same method will be used by clients of either interface. The
methods that implement an interface must be declared public. Also, the type
signature of the implementing method must match exactly the type signature
specified in the interface definition.
An example class that implements the Callback interface shown
earlier.
As shown in the figure given below, a class extends another class, an interface
extends another interface, but a class implements an interface.