Importing Packages
There are no core Java classes in the unnamed default package; all of the standard classes
are stored in some named package. Since classes within packages must be fully qualified
with their package name or names, it could become tedious to type in the long dot-separated
package path name for every class you want to use. For this reason, Java includes the import
statement to bring certain classes, or entire packages, into visibility. Once imported, a class
can be referred to directly, using only its name.
In a Java source file, import statements occur immediately following the package statement
(if it exists) and before any class definitions. This is the general form of the import statement:
import pkg1[.pkg2].(classname|*);
Here, pkg1 is the name of a top-level package, and pkg2 is the name of a subordinate
package inside the outer package separated by a dot (.). There is no practical limit on the
depth of a package hierarchy, except that imposed by the file system. This code fragment shows
use:
import [Link];
import [Link].*;
Example For creating a package and import statement
package MyPack;
public class Balance
{
String name;
double bal;
public Balance(String n, double b)
{
name = n;
bal = b;
}
public void show()
{
if(bal<0)
[Link]("--> ");
[Link](name + ": $" + bal);
}
}
As you can see, the Balance class is now public. Also, its constructor and its show( )
method are public, too. This means that they can be accessed by any type of code outside
the MyPack package. For example, here TestBalance imports MyPack and is then able to
make use of the Balance class:
import MyPack.*;
class TestBalance
{
public static void main(String args[])
{
Balance test = new Balance("J. J. Jaspers", 99.88);
[Link]();
}
}
Interfaces
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. 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.
Defining an Interface
An interface is defined much like a class. This is the general form of an interface:
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;
}
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
}
Example
Here is an example of an interface definition. It declares a simple interface that contains
one method called callback( ) that takes a single integer parameter.
interface Callback
{
void callback(int param);
}
Here is a small example class that implements the Callback interface shown earlier.
class Client implements Callback
{
public void callback(int p)
{
[Link]("callback called with " + p);
}
}
public static void main(String args[])
{
TestClass d = new TestClass();
[Link]();
}
}
Example: Multiple Inheritance
In the following example, we have two interfaces : Motorbike and Cycle. Motorbike
interface consists of the attribute speed. The method is totalDistance(). Cycle interface
consists of the attribute distance() and the method speed().
Both these interfaces are implemented by the class TwoWheeler
interface MotorBike
{
int speed=50;
public void totalDistance();
}
interface Cycle
{
int distance=150;
public void speed();
}
public class TwoWheeler implements MotorBike,Cycle
{
int totalDistance;
int avgSpeed;
public void totalDistance()
{
totalDistance=speed*distance;
[Link]("Total Distance Travelled : "+totalDistance);
}
public void speed()
{
int avgSpeed=totalDistance/speed;
[Link]("Average Speed maintained : "+avgSpeed);
}
public static void main(String args[])
{
TwoWheeler t1=new TwoWheeler();
[Link]();
[Link]();
}
}
Output
Total Distance Travelled : 7500
Average Speed maintained : 150