Previous
Index

Next


6.3) Interfaces

The theory of interfaces

Some Object Oriented programming languages (such as C++) implement multiple inheritance. This is where a class can inherit from more than one ancestor class. Implementing multiple inheritance is difficult for language designers and can cause confusion and complexity when creating programs. Most C++ programmers rarely if ever use multiple inheritance, so the designers of Java decided that it should only allow single inheritance. To allow for the circumstances where multiple inheritance is required Java uses a system called interfaces. A single Java class can implement multiple interfaces.

Using Interfaces

It is possible to write vast amounts of working Java code without ever creating an interface. Interfaces are more a design technique than an implementation tool, as they are designed to give information to other programmers. They are part of what is known as “design by contract”. An interface is a promise that certain functionality will be implemented in the concrete classes that implement them.

The interface keyword takes the abstract concept one step further. You could think of it as a “pure” abstract class. It allows the creator to establish the form for a class: method names, argument lists and return types, but no method bodies. An interface can also contain data members of primitive types, but these are implicitly static and final. An interface provides only a form, but no implementation.”
Bruce Eckel1997

(the term pure abstract is derived from the C++ language, so don't worry about that term if you are not familiar with C++)

Thus for example, you might have an interface for a User for a software system that has a promise that a method will be created for getting and setting the password. The methods will take the form

getPassword
setPassword



Note how each method ends with the parenthesis and has no body, or { /*body */} portion. On its own the interface does nothing as the methods have no bodies and thus no functionality. An interface is used when a programmer implements the interface and creates complete functions for all methods in the interface. So for example the following class might implement the user interface as follows

interface iUser{
	public String getPassword();
	public void setPassword(String sPassword);
}

class User implements iUser{
private String sPassword;
	public String getPassword(){
		return sPassword;
	}
	public void setPassword(String sPassword){
		this.sPassword=sPassword;
		}
}

Note that interfaces are designed to allow the equivalent of multiple inheritance, and for this purpose a class may implement multiple interfaces. To extend the previous example further you could have a student class that also implemented an called Person which would set out the “contract” for human beings. The following example illustrates how this might be done for a Student class (assuming that Students are assumed to be People).

/*
* Using a class that implements more
* than one interface
* @author Marcus Green 2006
*/

import java.util.*;
interface iPerson{
	public void setFirstName(String sName);
}

interface iUser{
	public void setPassword(String sPassword);
	public String getPassword();
}
/*
*note that a a class that implements a
*user interface can implement methods
*that are not in any of the interfaces
*thus this class has a setLastName method
*/
public class Student implements iUser,iPerson{
	private String sPassword;
	private String sFirstName;
	private String sLastName;
	public void setFirstName(String sName){
		this.sFirstName = sName;
	}
	public void setLastName(String lName){
		this.sLastName=lName;
	}
	public String getPassword(){
		return this.sPassword;
	}
	public void setPassword(String sPassword){
		this.sPassword=sPassword;
	}
}

Note that a class that implements an interface, must implement all the methods in that interface. If it does not implement one of the methods, (or quite typically, gets the signature slightly incorrect) a compile time error will occur. This makes sense as the purpose of an interface is to ensure that the implementing class exactly implements the interface as set out in the design.




A class that implements and interface must provide implementation for all of the methods in the interfaces

A classic example of using the interface is the java.lang.Runnable interface that is used with Threading. The JVM can be confident of calling the run method of a class that implements the Runnable interface without having to know anything about the actual content of that run method. Some interfaces are especially designed to have no methods at all and are known as Marker interfaces. As the name implies they are simply to Mark the implementing class as having some particular property.

References of an interface type

It may seem counter intuitive but it is possible to have a reference of a type that is an interface rather than a class. The following code demonstrates this.

/*
*@author Marcus Green
*creating a reference that has 
*a reference type
*/
interface MyInter{

}
public class InterfaceRef implements MyInter{
	public static void main(String argv[]){
		MyInter mi = new InterfaceRef();
	}
	public void amethod(){
	
	}
	
}

Note that the class reference created in this code would be rather useless as the method amethod could not be called from mi, as any method call would be of the interface type not of the object type. Because amethod is not implemented in MyInter you would get a compile time error.

How Interfaces are used

Interfaces are used in a similar way to classes, and are subject to the same naming restrictions. The convention is that they should begin with an upper case letter . By default interfaces can be implemented by all classes in the same package. However you can use the public keyword with an interface which will allow classes outside the pack3e to implement it.

Extending an interface

Just like classes can extend each other interfaces can extend each other using the extends keyword. As you might expect none of the classes in a chain of interface implementations can have methods with a body. An interface that extends another interface must have a bodyless version of all of the methods in its parent interface.

Interfaces and variables

Interfaces may contain final variables. This can be useful in that any classes that implement an interface with a final variable will have access to them. The following code demonstrates an interface with a final variable and a class that implements it.

/*
* @author Marcus Green
* An interface with a final variable
* and a class that implements and 
* uses it
*/
interface MyInter{
final int STARTTIME=10;

	public void amethod();
}
public class InterField implements MyInter{
	public static void main(String argv[]){
		
	}
	public void amethod(){
			System.out.println(STARTTIME);
	}
}

All variables within an interface are implicitly public final and static. Because of this the methods in the class that implement those of an interface must be explicitly marked as public. This is because the methods in an child class cannot be more private than those in from where it in herits.

A reference of a type that is an interface can be passed as an argument to a method. The following code demonstrates this.

/*
*@author Marcus Green
* create a method that has
* an argument of a type
* that is an interface
*/
interface MyInter{
	public void amethod(MyInter mi);
	public void showName(String sName);
}
public class IRefMethod implements MyInter{
	public static void main(String argv[]){
		MyInter mi = new IRefMethod();
		mi.amethod(mi);
	}
	/* note that amethod takes a parameter of the interface MyInter */
	public void amethod(MyInter mi){
		System.out.println("amethod");
	}
		public void showName(String sName){
		System.out.println(sName);
	}
	
}

Abstract Classes

Abstract classes are a little like interfaces in that they are mainly used by class designers. They allow the creation of a class that cannot be instantiated itself but can be extended by a child class.

abstract class Person{
	public abstract String getName();
	public abstract String setName(String sName);
}

class Student extends Person{
	private String sPassword;
	public String getPassword(){
		return sPassword;
	}
	
	public void setPassword(String sPassword){
		this.sPassword=sPassword;
	}

}

Note how each method is marked as abstract as well as the class itself. Unlike an interface where no method has a body, methods in an abstract class can have a body. The following example shows an abstract class that has implemented methods.

abstract class Person{
	String sPhoneNumber;
	public abstract String getName();
	public abstract String setName(String sName);
	public void setPhoneNumber(String sPhoneNumber){
			this.sPhoneNumber = sPhoneNumber;
	}
	public String getPhoneNumber(){
		return sPhoneNumber;
	}
} 




Previous
Index

Next