I have a theoretical question about the definition of inheritance. I had a test one month ago and we had to give an example for inheritance.
This was my answer:
public interface UserRepository extends CrudRepository<User, Long>
By definition the extend keyword indicates that the class which is being defined is derived from the base using inheritance. However, my professor graded this answer as wrong with the comment that my example is an interface extension and has nothing to do with inheritance.
What is the difference between an interface extension and inheritance? Why was my answer wrong, did I confuse something about the definition of inheritance?
CodePudding user response:
Words are tools used to convey meaning. If I say "The Student interface inherits from the Person interface", then the words I used are 'good', by definition, if everybody who hears me intuits immediately that we have:
public interface Student extends Person {}
And the word was bad by definition if some or all of the folks who hear me / read my text don't do that.
Period.
The problem is, what if the 'audience' is large and not easily enumerated? What if I write a blog post or SO answer that will build up a few hundred thousand views over the next 20 years? It's a bit hard to ask today if somebody who reads an answer 15 years hence actually correctly understands what I intended the words I used to convey.
That is the only point of whining about proper usage of terminology. There is no law that makes it illegal to call this 'inheritance', obviously. There are 4 useful sources for this:
- The audience's knowledge, if that is knowable. For an SO answer where it'll be read for the next 20 years, this isn't knowable. In a classroom, it is. In a conversation in a dev team, it definitely is. If everybody calls this kind of usage of the
extends
keyword, for some reason, "Student
ixtendsPerson
" (I made that up), then that's the word to use. The role of teaching here surely is to teach that which is most likely to convey your meanings to as broad an audience as is feasible, so this source is basically irrelevant. - An official definition. Official as in, official. Not 'my teacher said so', or 'this textbook said so'. That textbook is just written by somebody, not by some sort of Judge Jury Executioner. But specs carry some weight. So let's have a look!
If an extends clause is provided, then the interface being declared extends each of the other named interfaces and therefore inherits the member types, instance methods, and constants of each of the other named interfaces.
Pretty close: "inherits the member types", straight from the spec. I'd say your teacher is full of it, and is teaching their personal preferences as the only right answer. If you prefer, confront them about it, and make sure you print out §9.1.3 and show them.
To really rub it in, here's the relevant section about extend
ing class
definitions: JLS §8.1.4 - this doesn't mention the word "inheritance" at all, so going by spec your teacher is completely wrong.
Common 'slang' - what does Jane Q. Average Coder tend to assume the word means? I'm rather very sure that virtually every java programmer will intuit that the word "inheritance" at the very least means
class X extends Y
, and will at least be thinking: "... and probably alsointerface A extends B
, and perhaps evenclass X implements B
. In other words, going by this definition, if the distinction matters then terminology is irrelevant - saying "A inherits from B" when you aren't talking generally but want to specifically convey that A and B are classes and specifically not interfaces, then that sentence is not fit for purpose - it won't convey that idea. It doesn't matter what the JLS, your teacher, or any manual or book o' definitions says. This, clearly, cannot really determine an answer, though it strongly suggests you're right. To 'prove' it you'd have to, I dunno, go grab a mike and walk around a java conference, ask folks what they think it means.Some sort of generally agreed upon source of truth. A few books are so revered they are generally trusted; in other words, a book that is so trusted, that you win any argument almost immediately if you say: "... and [this hallowed book] agrees, see [chapter link here]". In java, maybe that's Effective Java, but books are annoying, in that you can't easily link to them in a way that anybody can verify (they'd have to buy the book first). I don't have a copy here with me right now but I highly doubt it picks any side here, it's not that kind of book.
... and that's it.
It's complicated, but, in essence, your teacher is just wrong here. It's stupid to mark this down. Giving said teacher the benefit of the doubt, they told you beforehand that the exercise involved remembering arbitrary meaningless stuff and you get graded on your ability to remember the arbitrary rules; the test has very little to do with java, it's more like a game of memory, with java concepts. There is some didactic basis for teaching like this, but you can perform such a test without having to resort to laying down the meaning of nebulous terms like 'inheritance' in such an overly precise fashion, so, whilst that's giving the benefit of the doubt, that means the grade-down is good, but their teaching methodology sucks.
Feel free to show this answer to them, and they can comment if they'd like to discuss the finer points.
CodePudding user response:
Well, the Java Language Specification § 9.1.3 mentions this:
If an extends clause is provided, then the interface being declared extends each of the specified interface types and therefore inherits the member classes, member interfaces, instance methods, and static fields of each of those interface types.
(Emphasis mine.)
So yes, I'm pretty sure this is inheritance.
CodePudding user response:
Maybe to give an example of inheritance we should point to something from CrudRepository
which is inherited by UserRepository
.