• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sierra/Bates Book Chapter 1 Question

 
Andrea Matellini
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Andrea Matellini
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic