Witscale Test Center

1.2 The Java class > 1.2.3 Details of interface declaration


1.2.3 Details of interface declaration

A Java Interface declaration is similar to Java class declaration with one difference. Since Java interface merely defines methods rather than implementing it, the interface body essentially has a set of method definitions that do not have a method body. The general format of the interface declaration is as shown below:

 

[modifiers] interface InterfaceName [extends SuperInterfaceList]

{

   [interfacebody]

}

 

The elements between the pair of square brackets [] are optional. At minimum, the interface declaration must contain the keyword interface along with the name of the interface followed by a pair of curly brackets. Additionally it can have the SuperInterfaceList. This can be a comma-separated list of interface names. Just like class, a Java interface too can extend another interface or it can even extend multiple interfaces. You need to to specicy such super interfaces in the SuperInterfaceList. Note that an interface is never allowed to extend a class. Therefore SuperInterfaceList can never have a class name. The interfacebody contains the interface method declarations. Let us see an example declaration for Interface Searchable with two methods:

 

public interface Searchable {

   public boolean search(String searchString) ;

   public boolean search(String searchString, boolean ignoreCase) ;

}

 

The above interface Searchable defines a common description for the search behavior. You may have several  classes in your application that are searchable. In that case, they share the common search behavior. If you want a class to be treated as a searchable, you need to implement the Searchable interface in that class. Let us see now what does it means to implement an interface.

 

You may wonder why to define an interface when it does not have any implementation. Note that interfaces serve a better cause while describing common behavior. You might have noticed that sometimes classes that are not directly related can share some common behavior. For instance, an AccountBook is searchable so is a Cupboard. If you have these two as classes then they should declare that they are searchable. They can do so by implementing the Searchable interface. Other classes interested only in searching (say for instance a Spy class) can treat them same. The Spy class can find out the methods that all the searchable classes have in common by simply looking at the Searchable interface definition. It can then call the search methods without worrying about other details of these classes.

 

How a class implements an interface

Earlier we saw how we could specify the interfaces the class intends to implement in a class declaration. Note that when a class declares that it will implement certain interface, it needs to implement all the methods specified in that interface. For instance, any class implementing the Searchable interface needs to implement the both search() methods. You can implement a method by providing a method body. For instance, if a class AccountBook implements the Searchable interface, it must implement its two methods. Let us declare a searchable class AccountBook as:

 

public class AccountBook implements Searchable {

 

   public boolean search(String searchString) {

             //Method body

   }

   public boolean search(String searchString, boolean ignoreCase) {

             //Method body

   }

   // Other methods

}

 

Note that this class declaration will not compile if AccountBook lacks either of the two search() methods. The implements clause in class declaration suggests that the class provides implementations for every interface method(s) declared by the Searchable interface. Note that a Java class is allowed to implement more than one interface. In the class declaration, you can specify the multiple interfaces as a comma-separated list of the interface names just after the implements clause. If a class thus declares to implement multiple interfaces, it must implement all methods specified in every interface.

 

Sometimes a class declares that it implements certain interface (or interfaces) but it may not actually implement all the expected methods. In such case, you must declare the class as abstract. Such class has a partial implementation and defers the remaining implementation to its subclasses. We will discuss the abstract classes in chapter 3.

 

Now that we know how-to syntactically represent a Java class, let us see how to represent it pictorially.