Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes k&B SCJP 5 misleading in explaining inheriting interface impl. method 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 "k&B SCJP 5 misleading in explaining inheriting interface impl. method" Watch "k&B SCJP 5 misleading in explaining inheriting interface impl. method" New topic
Author

k&B SCJP 5 misleading in explaining inheriting interface impl. method

Saskia de Jong
Ranch Hand

Joined: Jan 24, 2006
Posts: 34
On page 115 of k&b SCJP 5 the following is stated (I quote freely):

"[Take] a class that declares it implements an interface [...] but doesn't implement the methods [...] if any [super] class has provided method implementations AND has declared that it implements the interface, then [this] subclass is under no obligation to re-implement those methods"

Technically this is correct. However, I think putting it this way is slightly misleading. The reader might think the superclass HAS to declare the interface too, but this does not need to happen at all.

As an example, the following code compiles with no errors:








The class "SubClass" uses the inherited method foo() to implement the FooInterface, without any declaration of the FooInterface within the class that defines this method. Of course the code still compiles if "SuperClass" had indeed declared the interface, but as the example shows this is not required at all.

It seems to me that it might actually be more accurate if the k&b book mentions that the AND part in the mentioned quote is just an optional thing. Currently it really sounds (at least to me) as a requirement.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You are correct - this looks like an error. The phrase "AND has declared that it implements the interface" should simply be removed.


"I'm not back." - Bill Harding, Twister
Saskia de Jong
Ranch Hand

Joined: Jan 24, 2006
Posts: 34
Ok, thanks for the quick reply Jim!
MI Mohammed
Ranch Hand

Joined: Feb 16, 2005
Posts: 146
Posted by Jong
[Take] a class that declares it implements an interface [...] but doesn't implement the methods [...] if any [super] class has provided method implementations AND has declared that it implements the interface, then [this] subclass is under no obligation to re-implement those methods"


I do agree with Jim, but I strongly feel the and should be change to or.

public interface FooThing {
public void doFoothings();
}

public class Super implements FooThings {
public void doFooThings() {}
}

public class Sub extends Super {
// doFooThins() is inherited,
// Sub does not have to implement FooThings methods.
}
or

public class Sub extends Super implements FooThing {
// same result
}


SCJA(Beta) SCJP 1.4 SCWCD 1.4 SCBCD 1.3 SCBCD 5.0 beta <br />The more practice we get, the better we are at the exams and in life in general. Pls join me at My DEN.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Mmmm, but that doesn't make sense. If we replace AND with OR, we get:

Take a class that declares it implements an interface but doesn't implement the methods [...] if any [super] class has provided method implementations OR has declared that it implements the interface, then this subclass is under no obligation to re-implement those methods

When you have a statement of the form "if A or B then C", that implies

I. if A then C

II. if B then C

In this specific case, replacing AND with OR implies:

I. Take a class that declares it implements an interface but doesn't implement the methods [...] if any [super] class has provided method implementations, then this subclass is under no obligation to re-implement those methods

II. Take a class that declares it implements an interface but doesn't implement the methods [...] if any [super] class has declared that it implements the interface, then this subclass is under no obligation to re-implement those methods

Statement I is true, but statement II is false. Assuming we want to be able to instantiate this class, of course. It's possible to make an abstract class that satisfies condition II, but I don't think that's what was meant. It would be much better to drop statement II entirely. It really doesn't matter whether or not the superclass declares it implements the interface, if the subclass declares it. Well, it would matter to the superclass, which is forced to be abstract if it declares the interface without implementing it. But it's irrelevant to the subclass.
gaurav singhal
Ranch Hand

Joined: Nov 18, 2005
Posts: 135
Thanks jim and saskia for giving a gr8 input on it bcoz after reading K&B I also understood that a superclass has to implement the interface. But now I know it is not true...it is not copulsory for superclass to implement the interface.

Thanks for the clarifications
Dario Laverde
Greenhorn

Joined: Feb 27, 2006
Posts: 16
Hello all (new poster here),

I discovered this same error 2 weeks ago while going over the book with a local Java Users Group (NYC) and decided to clarify the issue by simply dropping the second part of the statement since it is not required (instead of replacing the AND with an OR which does introduce the issue Jim addresses above).

BTW - is there an errata yet for this book?

Despite this error, I'd like to commend the authors, Kathy and Bert for an excellent book (the best certification book we've used yet) for it's thorough coverage and excellent review questions.

For those in the NYC area studying for the exam and would like to get together with others to study using the K&B book feel free to stop by our users group. Info available at nycjava.net

regards,
Dario Laverde
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Hi Guys,

Here's my proposal...

First off, I'd say that the sentence in question isn't an error, but that it certainly could be improved.

The current sentence is:

If any class in its inheritance tree has already provided concrete (i.e., non-abstract) method implementations, and has declared that it (the superclass) implements the interface, then the subclass is under no obligation to re-implement (override) those methods.


I'd say that this sentence is correct, but that it leaves out the possiblity that you're discussing.

So, here's a proposed new sentence:

If any superclass in its inheritance tree has already provided concrete (i.e., non-abstract) method implementations, then regardless of whether the superclass implements the interface, the subclass is under no obligation to re-implement (override) those methods.


What do you think?


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Bert]: First off, I'd say that the sentence in question isn't an error

Agreed. In the same sense that, "If it's Thursday, the Pope is Catholic" isn't technically an error.

What do you think?

Looks good. I might replace

"regardless of whether the superclass implements the interface"

with

"regardless of whether the superclass declares that it implements the interface"

Technically that's not necessary, but if the space is available I think it removes a potential source of confusion.
[ February 28, 2006: Message edited by: Jim Yingst ]
Abhishek Misra
Ranch Hand

Joined: Sep 26, 2005
Posts: 68
that makes my head spin. please add an example in there to make more sense.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Abhishek - unfortunately space is usually limited on errata, because they mustn't change the page numbers for the other text; that would force reformatting and reindexing large portions of the book. I'm pretty sure they won't be able to insert an example at this point. The version I suggested earlier - dropping the rest of the sentence starting with AND - is probably simpler to understand on first read. But for those people who may be wondering whether it matters if the superclass implements the interface, Bert's version answers that question. So, is it better to raise this issue and answer it, or to omit the issue entirely and keep things simpler?
Abhishek Misra
Ranch Hand

Joined: Sep 26, 2005
Posts: 68
Wha Jim suggested is more brain friendly.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
hmmm...

now I have to go back to the drawing board because I'm going to guess that, in fact, a couple of different superclasses could each implement parts of the interface and you'd still be okay...

anyone wanna do an experiment?
Abhishek Misra
Ranch Hand

Joined: Sep 26, 2005
Posts: 68
Bert you and Kathy are good at drawings, you don't need help ). i would love to see those drawings in next errata or revision
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Bert]: now I have to go back to the drawing board because I'm going to guess that, in fact, a couple of different superclasses could each implement parts of the interface and you'd still be okay...

Certainly.

Seem like you could try to list all the various possible permutations of this idea, or you could just keep it simple and trust that anyone smart enough to wonder about the special cases can also test them themselves.

I don't have a copy of the book, so I can't see the full context, but I trust that something in the text will still establish that the subclass does declare it implements the interface? That info was in Saskia's original paraphrase, but is not in Bert's replacement sentence, so I hope it's somewhere in the surrounding context.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
That seems like a side issue. There are many additional rules about how to legally implement a method, and how to write classes and methods in general. Which of these rules must we restate here? We could remind the reader that the return type must be either the same as, or a subtype of, the original declared return type. We could point out that the list of exceptions thrown by the method may not contain any checked exceptions which were not listed in the original method, either explicitly or via supertype. We could remind people that the names of method parameters are not part of the signature, and may be different in an implementing method. Or that an implementing method may not be static. Or that it may be final or synchronized. Or any number of other rules which come into play when writing a class or method. I really don't think it's necessary or practical to get into all these. The main point of the sentence has already been stated; it shouldn't be necessary to bog the sentence down with all the other rules that also come into play. That's what the rest of the book is for.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I think that's shifting the focus of the sentence a bit, and is less clear at achieving Bert's original goals. I'm not really sure what need is being addressed here. There are many, many, many different things that one could talk about in any given sentence, but what's the goal? I think Bert's revised form works fine for what he wanted to convey. This is what appeared in Bert's recent "temporary errata":
If any superclass in its inheritance tree has already provided concrete (i.e., non-abstract) method implementations, then, regardless of whether the superclass declares that it implements the interface, the subclass is under no obligation to re-implement (override) those methods.

The original misleading text is gone. Great. That's addressed the problem I cared about. Beyond that, I'm not sure what else needs to be said.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: k&B SCJP 5 misleading in explaining inheriting interface impl. method