Java Interfaces

The abstract class BeeperPutter has no concrete methods at all. A better solution here in Java would be to use an interface instead of an abstract class. An interface is like an abstract class except that all of its methods are automatically public and abstract.

interface BeeperPutter 
{	void distributeBeepers();
}

The reason that this is superior is that it gives more flexibility. A Java class can extend only one other class, but it can implement several interfaces. If we define BeeperPutter this way then the NoNeighbor class becomes:

public class NoNeighbor extends ur_Robot implements BeeperPutter
{	public void distributeBeepers()
	{	putBeeper();
		move(); 
	}
   
}

While NoNeighbor implements only one interface here, it could implement many instead. The NeighborTalker class would have a similar definition. Otherwise the program would be the same as in the main text. In particular, we can declare a robot to be of "class" BeeperPutter. What this means is that it is in some (any) class that implements the BeeperPutter interface.

Notice, however that an interface doesn't extend any class, though it may implement other interfaces. Therefore in NoNeighbor we needed to give a class that this new one extends as well as the new interface that it implements.

Interfaces can also have many (unimplemented) methods. Here we show only one. Interfaces can also define constants, but it is too early to discuss these.