This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sierra/Bates Book Chapter 1 Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sierra/Bates Book Chapter 1 Question" Watch "Sierra/Bates Book Chapter 1 Question" New topic
Author

Sierra/Bates Book Chapter 1 Question

Andrea Matellini
Greenhorn

Joined: Nov 09, 2009
Posts: 2
Hi,

in self test of chapter 1 of this great book I have found a strange question, the number 6.


1. public class Electronic implements Device
{ public void doIt() { } }
2.
3. abstract class Phone1 extends Electronic { }
4.
5. abstract class Phone2 extends Electronic
{ public void doIt(int x) { } }
6.
7. class Phone3 extends Electronic implements Device
{ public void doStuff() { } }
8.
9. interface Device { public void doIt(); }

This code will compile?

The answer is yes but I do not understand why.

I have tried it and compile fine, but line line 7 is a little strange for me because Phone extend Electronic that implement already the interface Device.
This little ambiguos for me to implement two time the same interface. As a test I have also tried to implement twice the same interface in a class and the compiler say "Duplicate interface Device for the type Electronic". There are some reference in the Java Lang Spec that explain this behaviour?

Thanks
Ravinderjit Singh
Ranch Hand

Joined: Jan 13, 2010
Posts: 78
Can you please post the code which you are trying to compile and make sure you put your code in CODE tag.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Andrea, what you are asking is allowed for the sake of clarity. If class Phone3 wants to clearly mention that it implements the Device interface, it can do so even if it is already a sub-class of Electronic which implements Device. So now if anyone looks at the class Phone3, they know that Phone3 implements Device, they don't have to look at class Electronic to know that.

Also if you try something like this, you are bound to get an error because this is useless


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Andrea Matellini
Greenhorn

Joined: Nov 09, 2009
Posts: 2
Thanks Ankit for your answer.

This is the code in CODE tag requested by Ravinderjit, sorry this is my first post . This compile fine. The only "problem" I have found is that I have not found in the book (and in Java lang spec) that a class may implement an interface already implemented by a superclass.






rushikesh sawant
Ranch Hand

Joined: Dec 22, 2009
Posts: 65
Andrea, in that example class is not implementing Device interface again as it is already implemented by its super class.
If a super class implements an interface then all of its subclasses are also implementers of that interface. There is no need to say "implements" again for those subclasses.
Means in this code:


There is no need to define that Phone1 implements Device interface again explicitly. It is inherently implementing it.

SCJP 5.0 100%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sierra/Bates Book Chapter 1 Question