Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface

 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From JLS 9.1.2 Superinterfaces and Subinterfaces

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.
 
Brian Lugo
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know there are only three correct answers to this question, but what is wrong with the choice a.
Thanks,
Brian
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Brian Lugo
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it - Thanks a lot Val!
Brian
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just one more thing:
consider the following code:

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic