12.packages and Interfaces
12.packages and Interfaces
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, awutil, sql etc.
Built-in packages
The package keyword is used to create a package in java.
//save as Simple.java
package mypack;
public class Simple{
public static void main(String args[])
{
System.out.println("Welcome to package");
}
}
How to access package from another package?
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.
1) Example of package that import the packagename.*
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");} Output:Hello
}
---------------------------------------------------------------------------------
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
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; Output:Hello
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(); } }
Subpackage in java
Package inside the package is called the subpackage. It should be
created to categorize the package further.
package vehicles;
package vehicles.cars;
import vehicles.Vehicle;
public class Car extends Vehicle {
@Override
public void displayType() {
System.out.println("I am a Car");
}}
------------------------------------------------------------------------------
vehicles/trucks/Truck.java (with overridden method):
package vehicles.trucks;
import vehicles.Vehicle;
public class Truck extends Vehicle {
@Override
public void displayType() {
System.out.println("I am a Truck");
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.
Syntax:
interface <interface_name>{
// declare constant fields
// declare methods that abstract by default.
}
Example 1: Defining and Implementing an Interface
//Defining an Interface:
interface Camera
{
void takePhoto();
}
interface MusicPlayer
{
void playMusic();
}
Implementation Class:
Interfaces:
interface Walker { Output:
void walk();
} Duck is walking.
interface Swimmer { Duck is swimming.
void swim();
}
Implementation Class:
class Duck implements Walker, Swimmer {
public void walk() {
System.out.println("Duck is walking.");
}
public void swim() {
System.out.println("Duck is swimming.");
}
public static void main(String[] args) {
Duck myDuck = new Duck();
myDuck.walk();
myDuck.swim();
Interfaces
Vs
Abstract classes
Abstract class Interface
1) Abstract class can have abstract Interface can have only abstract methods. Since
and non-abstract methods. Java 8, it can have default and static
methods also.
3) Abstract class can have final, Interface has only static and final variables.
non-final, static and non-static
variables.
4) Abstract class can provide the Interface can't provide the implementation of
implementation of interface. abstract class.