|
There are three prominent rules about interface implementation .The first two are about the declaration of classes and interfaces. The third rule is about implementation of interface methods.
A class can implement multiple interfaces.
A class can play multiple roles by specifying multiple interfaces in the implements clause. However, a class cannot extend from an interface. In other words, you cannot specify an interface name after the extends clause in class declaration. For example, assume that we have two interfaces Pianist and Poet as:
public interface Pianist {
void playPiano();
}
public interface Poet {
void writeAPoem();
}
Table 8.3 shows some valid and invalid declarations of classes. A class can implement interface using only the implements clause.
Table 8.3 Valid and invalid declarations for implementing class
|
Using |
Implementing class |
|
|
Valid declarations |
Invalid declarations |
|
|
implements clause |
public class Person1 implements Pianist{ void playPiano() {} }
public class Person2 implements Pianist, Poet { void playPiano() {} void writeAPoem() {} } |
public class InvalidPerson1 implements Person { } |
|
extends clause |
|
public class InvalidPerson2 extends Pianist {} |
Classes Person1 and Person2 implements interface correctly with the implements clause. The class InvalidPerson1 will not compile as it is trying to implement a class Person. You are never allowed to implement a class, only interface should be implemented. The class InvalidPerson2 also will not compile because it is trying to extend an interface. You can extend a class from only another class.
An interface can extend from only another interface.
You can extend an interface from another interface just like you extend a java class from another java class. Note that, an interface can only be extended from another interface. You cannot extend an interface from a class. Another difference between extending a class and extending an interface is that a class can be extended from only one class but an interface can be extended from multiple interfaces. Table 8.4 shows some valid and invalid interface declarations.
Table 8.4 Valid and invalid declarations for an interface
|
Using |
Extending an interface |
|
|
Valid declarations |
Invalid declarations |
|
|
extends clause |
public interface Composer1 extends Pianist { void compose(); } |
public interface InvalidComposer1 extends Person { void compose(); } |
|
public interface Composer2 extends Pianist, violinist { void compose(); } |
|
|
|
implements clause |
|
public interface InvalidComposer2 implements Pianist{ void compose(); } |
Interfaces Composer1 and Composer2 are declared correctly. Composer1 extend another interface correctly with the extends clause. Interface Composer2 extends from multiple interfaces. The interface InvalidComposer1 will not compile as it is trying to extend a class Person. You are not allowed to extend a class to declare an interface; you can extend an interface only from another interface. The class InvalidComposer2 will not compile as well. It is using implements clause in interface declaration which is not correct.
A class implementing an interface must provide implementation of all interface methods.
We learned earlier that all methods of an interface are abstract by default. When a java class says that it implements particular interface, it must implement all the methods in that interface or should declare it self as abstract. If the class is declared as abstract, the subclasses provide the implementation for the interface methods. So in either way the implementation of the interface methods is guaranteed. LEt us consider an interface OfficeManager with 4 methods as:
public interface OfficeManager {
void supervision();
void reporting();
void drafting();
void payroll();
}
The table 8.5 shows some valid and invalid implementation of the interface OfficeManager.
Table 8.5 Valid and invalid implementations of interface
|
Implementing an interface |
|
|
Valid declarations |
Invalid declarations |
|
public class Employee extends Person implements OfficeManager { void supervision() {} void reporting(){} void drafting(){} void payroll(){} } |
public class InvalidEmployee extends Person implements OfficeManager { } |
|
public abstract class Robot implements OfficeManager { void payroll(){} } |
public class InvalidRobot implements OfficeManager { void drafting(){} void payroll(){} } |
Class Employee implements the interface OfficeManager appropriately, as it provides implementation to all the four methods of that interface. It does not matter that the implementing methods have empty bodies. The class Robot also implement OfficeManager correctly. Even if it does not implement all four methods of OfficeManager, it is valid implementer as it is declared as abstract. Since its subclasses may implement the rest of the methods, .
InvalidEmployee and InvalidRobot do not implement OfficeManager correctly as they do not give implementation to all of its methods.
|
|
The objective 4.2 specifically states interface java.lang.Runnable. The implementation rules are same for all java interfaces. We will learn more about this particular interface in chapter 12 (Threads). For now remember that it has a single method (void run();) and the classes implementing it should provide implementation for this method. |