Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Saskia de Jong
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct - this looks like an error. The phrase "AND has declared that it implements the interface" should simply be removed.
 
Saskia de Jong
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for the quick reply Jim!
 
MI Mohammed
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
}
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that makes my head spin. please add an example in there to make more sense.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wha Jim suggested is more brain friendly.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic