Java
Table of Contents

Interfaces and Packages

Packages

Packages are groups of related classes. Packages help in organizing your code and provide another layer of encapsulation. It is just like the way folders help in organizing files.Classes within a package can be accessed by first specifying the package name

Consider the following snippet of code

package packageA;

This statement when placed at the head of a Java source file will force all the classes defined in that source file to belong to the packageA. Java uses the underlying file system to manage packages, with each package stored in its own directory i.e name of the directory and package should match.For example, the .class files for any classes you declare to be part of packageA must be stored in a directory called packageA.

We can have nested packages as well. For e.g

package packageA.packageAA.packageAAA

will store all the class files having this statement at the head in the /packageA/packageAA/packageAAA folder.

// A short package demonstration.
package BookPackage;

class Book {
  private String title;
  private String author;
  private int pubDate;

  Book(String t, String a, int d) {
    this.title = t;
        this.author = a;
        this.pubDate = d;
        }

    void show() {
            System.out.println(title);
                System.out.println(author);
                System.out.println(pubDate);
                System.out.println();
        }
    }

class BookDemo {
  public static void main(String args[]) {
    Book books[] = new Book[5];
    books[0] = new Book("Java: A Beginner's Guide","Schildt", 2005);
    books[1] = new Book("Java: The Complete Reference","Schildt", 2005);
    books[2] = new Book("The Art of Java","Schildt and Holmes", 2003);
    books[3] = new Book("Red Storm Rising","Clancy", 1986);
    books[4] = new Book("On the Road","Kerouac", 1955);
    for(int i=0; i < books.length; i++) books[i].show();
  }
}

Since the class file is now bound to the package name we will have to put the class file in the BookPackage Directory under the project home, and we will have to run the program in this way.

java BookPackage.BookDemo

Interface

An interface put in the simplest of words is a a collection of unrelated method names(functions) followed by the list of parameters. The methods will not have any implementation. An Interface is a logical construct.

Consider the following example

public interface Bicycle {
           void changeCadence(int newValue);
           void changeGear(int newValue);
           void speedUp(int increment);
           void applyBrakes(int decrement);
    }

Here we have the interface name Bicycle following the key word interface, the body of this interface consists of a set of unrelated functions which have only a prototype/signature but no definition. It is upto the class that implements the interface to also implement the function definition. This is how the implementation of the function will look

class MyBicycle implements Bicycle {
    void speedUp(int increment){
            //Body of the function
        }

}

A Class may at any point of time implement multiple interfaces. As interfaces themselves are abstract they cannot be instantiated. The major disadvantage of interfaces is that if a class implements an interface then it has to provide a concrete implementation for all the methods in that interface .

The following 2 file implement a simple queue using interfaces. They use a package called interfacepackage and hence all the files should be in the folder interfacepackage on the filesystem.
QueueInterface.java
QueueDemo.java

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License