Dear all, i have a question from a mock exam Which of these statement about interface are true? Select 3 correct answer a. Interfaces permit multiple implementation inharitance b. Unlike a clas, an interface can extend from multiple interfaces c. Members of an interface are never static d. Members of an interface may be static e. Interfaces cannot be final The answer is b, d, e Can you help me why b is true? daniel
If an extends clause is provided, then the interface being declared extends each of the other named interfaces and therefore inherits the member types, methods, and constants of each of the other named interfaces. These other named interfaces are the direct superinterfaces of the interface being declared. Any class that implements the declared interface is also considered to implement all the interfaces that this interface extends.
in clear: interface A{} interface B{} interface C extends A,B{} is perfectly legal.
interface do not provide multiple implementation inheritance since interfaces do not contain any implementation. Java is based upon single implementation inheritance, that is, a class is allowed to directly subclass only one class at a time (unlike in C++). However, multiple interface inheritance is allowed as described in the JLS sections posted above. [ March 13, 2002: Message edited by: Valentin Crettaz ]
By looking at this code, one might wonder why multiple interface inheritance works and may ask him/herself "but which one of the methodA gets executed?". Well, since interfaces do not contain any implementation, interface methods are not executed. Interface methods must be implemented by implementing classes (here Test). Paragraph 8.4.6.4 Inheriting Methods with the Same Signatureexplains what happens when several methods having the same signature are inherited from multiple interfaces. Remember that interface methods are implicitely abstract, and if your class (Test) is not abstract it does necessarly mean that it overrides (and implements) the methods (methodA) it inherits from its superinterfaces (A and B), thus the problem is solved.