Why 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).

import java.util.*;
interface iPerson{
	public String getName();
	public String setName(String sName);
}

interface iUser{
	public String getPassword();
	
}

class Student implements iUser{
	private String sPassword;
	public String getPassword(){
		return 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.

Last modified: Sunday, 20 September 2015, 07:21 PM