• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help on INTERFACES

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone kindly tell me if an interface can extend or implement multiple interfaces ? If so, why is this allowed only for interfaces ?
Thanx in Advance !
 
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
An interface can extend several interfaces but not implement them since interfaces do not contain any implementations. In clear, you'll never see something like this:
interface A {}
public interface B implements A {}
Interfaces are allowed to extend multiple interfaces because the problem with multiple inheritance is not with the method signatures but with their implementations. If a method is inherited from two classes, which method should be chosen, that is, which method body should be executed? Since interfaces only contain methods declarations, there is no problem in "inheriting" two similar declarations from two distinct interfaces.
Please check out JLS 8.1.4 Superinterfaces for an example.
 
Gokulakrishnan Raghuraman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx for answering. But can you kindly follow the following link leading to java prepare's sample test ... at qn no 36, there is a question referring to our discussion ... but the answer for that seems to be contrary to what we discussed now...
link is
http://www.javaprepare.com/quests/test.html
 
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 to be clear, here is the question 36 from Java Prepare:


Which of the following are true about interfaces. Select the two correct answers.
A. Methods declared in interfaces are implicitly private.
B. Variables declared in interfaces are implicitly public, static, and final.
C. An interface can extend any number of interfaces.
D. The keyword implements indicate that an interface inherits from another.
Answer: B,C


A is clearly wrong as per JLS 9.4 Abstract Method Declarations
B is, in my opinion, badly worded and should read "Fields declared in interfaces are implicitly public, static, and final." Interface can only declare constants. In this respect, B is correct.
C is clearly correct as per JLS 9.1.2 Superinterfaces and Subinterfaces
D is clearly wrong as the keyword "implement" can only occur in class declarations and not in interface declarations as per JLS 18.1 The Grammar of the Java Programming Language (in the "ClassDeclaration" production).
Try to compile to following code and you'll see:

It should give you the following error:
expected "{", found implements
The error message may vary depending on the compiler you are using.
 
Gokulakrishnan Raghuraman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a lot for explaining the details.
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic