aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes help on INTERFACES Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "help on INTERFACES" Watch "help on INTERFACES" New topic
Author

help on INTERFACES

Gokulakrishnan Raghuraman
Greenhorn

Joined: Nov 27, 2002
Posts: 10
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 !
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Gokulakrishnan Raghuraman
Greenhorn

Joined: Nov 27, 2002
Posts: 10
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
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Nov 27, 2002
Posts: 10
Thanx a lot for explaining the details.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help on INTERFACES