Introduction to Packages
A package is a mechanism to group the similar type of classes, interfaces and sub-packages and provide access control. It organizes classes into single unit.
In Java already many predefined packages are available, used while programming.
For example: java.lang, java.io, java.util etc.
Advantages of Packages
- Packages provide code reusability, because a package has group of classes.
- It helps in resolving naming collision when multiple packages have classes with the same name.
- Package also provides the hiding of class facility. Thus other programs cannot use the classes from hidden package.
- Access limitation can be applied with the help of packages.
- One package can be defined in another package.
Types of Packages
There are two types of packages available in Java.
1. Built-in packages
Built-in packages are already defined in java API. For example: java.util, java.io, java,lang, java.awt, java.applet, java.net, etc.
2. User defined packages
The package we create according to our need is called user defined package.
Creating a Package
We can create our own package by creating our own classes and interfaces together. The package statement should be declared at the beginning of the program.
Syntax:
package <packagename>;
class ClassName
{
……..
……..
}
Example: Creating a Package
// Demo.java
package p1;
class Demo
{
public void m1()
{
System.out.println("Method m1..");
}
}
How to compile?
Syntax: javac –d directory javafilename
For Example: javac –d . Demo.java
How to run?
To run: java p1.Demo
Example: Program to create and use a user defined ackage in Java.
// Vehicle.java
package vehicles;
interface Vehicle
{
public void run();
public void speed();
}
//Bike.java
package vehicles;
public class Bike implements Vehicle
{
public void run()
{
System.out.println("Bike is running.");
}
public void speed()
{
System.out.println("Speed of Bike: 50 Km/h");
}
public static void main(String args[])
{
Bike bike = new Bike();
bike.run();
bike.speed();
}
}
Compile:
javac –d . Vehicle.java
javac –d . Bike.java
Run:
java vehicles.Bike
Output:
Bike is running
Speed of Bike: 50 Km/h
The “import” keyword
The import keyword provides the access to other package classes and interfaces in current packages.
“import” keyword is used to import built-in and user defined packages in java program.
There are different 3 ways to access a package from other packages.
1. Using full qualified name
Example
class Demo extends java.util.Scanner
{
//statements
}
2. import only single class
Example
import java.util.Scanner;
class Demo
{
// statements
}
3. import all classes
Example
import java.util.*;
class Demo
{
// statements
}
Interface
- Interface is similar to a class, but it contains only abstract methods.
- By default the variables declared in an interface are public, static and final.
- Interface is a mechanism to achieve full abstraction.
- An interface does not contain any constructor.
Syntax:
interface InterfaceName
{
public void method1();
public void method2();
<type> variableName = value;
}
Example: Sample of an interface
interface Employee
{
static final Id = 101;
static final String name = “ABC”;
void show();
void getSalary(double salary);
}
Extending interfaces
An interface has to extend another interface as it happens in class. It cannot implement another interface.
Example: Sample program for extending interfaces
interface Base
{
public void display ();
}
interface Derive extends Base
{
public void show ();
}
Implementing interfaces
- A class implements an interface. After that, class can perform the specific behavior on an interface.
- The implements keyword is used by class to implement an interface.
Syntax:
class ClassName implements interfacename
{
// body of class
}
Note: A class can implement more than one interface. Java can achieve multiple inheritances by using interface.
Example: Sample program to implements interface in Java
interface Results
{
final static float pi = 3.14f;
float areaOf(float l, float b);
}
class Rectangle implements Results
{
public float areaOf(float l, float b)
{
return (l * b);
}
}
class Square implements Results
{
public float areaOf(float l, float b)
{
return (l * l);
}
}
class Circle implements Results
{
public float areaOf(float r, float b)
{
return (pi * r * r);
}
}
public class InterfaceDemo
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Square square = new Square();
Circle circle = new Circle();
System.out.println("Area of Rectangle: "+rect.areaOf(20.3f, 28.7f));
System.out.println("Are of square: "+square.areaOf(10.0f, 10.0f));
System.out.println("Area of Circle: "+circle.areaOf(5.2f, 0));
}
}
Output:
Area of Rectangle: 582.61
Are of square: 100.0
Area of Circle: 84.905594
Example: Sample program to implements multiple inheritance
interface Vehicle
{
void run();
}
interface Bike extends Vehicle
{
void stop();
}
public class Demo implements Bike
{
public void run()
{
System.out.println("Vehicle is running.");
}
public void stop()
{
System.out.println("Bike is stop.");
}
public static void main(String args[])
{
Demo obj = new Demo();
obj.run();
obj.stop();
}
}
Output:
Vehicle is running.
Bike is stop.
Marker Interface
An interface which does not contain any fields and methods is known as marker or tag interface. It is an empty interface.
For example Serializable, EventListener, MouseListner, Remote, Cloneable etc.
Example:
package java.util;
public interface EventListner
{
}
Differences between Abstract class and Interface
Abstract class |
Interface |
It cannot support multiple inheritances. |
Interface supports multiple inheritances. |
It contains both abstract and non abstract method. |
It contains only abstract method. |
Abstract class is the partially implemented class. |
Interface is fully unimplemented class. |
It can have main method and constructor. |
It cannot have main method and constructor. |
It can have static, non-static, final, non-final variables. |
It contains only static and final variable. |
0 Comments