Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Interface

 
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it - Thanks a lot Val!
Brian
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic