Saturday, February 2, 2013

Packages and Interfaces programs

                                                          Packages and Interfaces
13. Write a java program that illustrates the following:
(a) Creation of simple package.
(b) Accessing a package.
(c) Implementing interfaces.

Packages are a feature of Java that helps you to organize and structure your classes and their relationships to one another. A package is a grouping of related types providing access protection and name space management. Note that types refer to classes, interfaces and others. The types that are part of the Java platform are members of various packages that bundle classes by function: fundamental classes are in java.lang, classes for reading and writing (input and output) are in, and so on. You can put your types in packages too.
For example, consider the following package specification:

package MyPack;

In order for a program to find MyPack, we use one of the two alternatives:

1.Program is executed from a directory immediately above MyPack, or

2.CLASSPATH must be set to include the path to MyPack.
We create a directory, MyPack below the current development directory, put the .class files into the MyPack directory, and then execute the program from the development directory.

Program (a): A simple package example
package MyPack; // A simple package
class Balance
String name;
double bal;
Balance(String s, double b) // constructor
{ name = s;
bal = b;
void show() // method
  if(bal < 0)
        System.out.print("-->> ");
   System.out.println(name + ": Rs" + bal);
class AccountBalance
public static void main(String args[])
Balance current[] = new Balance[3];
current[0] = new Balance("R. Lepakshi", 15230.50);
current[1] = new Balance("K. Siva Kumar", 350.75);
current[2] = new Balance("Michael Jackson", -120.30);
for(int i = 0; i < 3; i++) current[i].show();

Let the current development directory be C:\java

Create (make directory: md) MyPack directory as follows:

C:\java>md MyPack
Edit the program and compile it from current development directory as follows:



Then, copy Balance.class and AccountBalance.class files from the directory C:\java to the directory C:\java\MyPack

Execute the AccountBalance class, using the following command line:
C:\java>java MyPack.AccountBalance

AccountBalance is now part of the package MyPack. This means that it cannot be executed by itself. That is, we cannot use this command line:
C:\java>java AccountBalance

AccountBalance must be qualified with its package name.

In Java, an interface is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated - they can only be implemented by classes or extended by other interfaces.
Here is an example of an interface definition (defining an interface is similar to creating a new class). Program 13(b) declares an interface called Items which contains four methods. Note that the method signatures have no braces and are terminated with a semicolon.

Program (b): Defining an interface Items

interface Items
   boolean equal(); // test whether two numbers are equal
  int add(); // sum of two numbers
  int max(); // maximum of two numbers
  void display(); // print two numbers

To declare a class that implements an interface, you include an implements clause in the class declaration. Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. When an instantiable class implements an interface, it provides a method body for each of the methods declared in the interface. Here is an example class Client that implements the Items interface.

Program (c): Implementing an interface Items
class Client implements Items
int a, b;
Client( int i, int j) // constructor
{ a = i; b = j; }
public boolean equal()
{ return (a == b); }
public int add()
{ return(a + b); }
public int max()
{ if( a > b ) return a;
else return b;
public void display()

  System.out.println(a + " " + b);
public int multiply() // non-interface method
    return(a * b); }

Program (d): Testing a class which implements an interface

class ClientDemo
 public static void main(String[] srgs)
  Client ob = new Client(5, 7);
if( ob.equal())
  System.out.println("Two numbers are equal");
  System.out.println("Two numbers are not equal");
  System.out.println("Sum of two numbers is " + ob.add());
  System.out.println("Max of two numbers is " + ob.max());
 System.out.println("Product of two numbers is " + ob.multiply());

Output of this program is:
5 7

Two numbers are not equal
Sum of two numbers is 12
Max of two numbers is 7
Product of two numbers is 35

No comments:

Post a Comment