2

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?

linuxman
  • 147
  • 1
  • 1
  • 8
  • `class which is being defined` Why did you mention class? An interface can extend one or more interfaces and inherits their methods apart from private (also note that *static* members of the interface or class can **not** be inherited by definition). – Alexander Ivanchenko Feb 20 '22 at 22:01
  • I mentioned class, because both interfaces and classes are types. – linuxman Feb 20 '22 at 22:04

3 Answers3

3

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.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
2

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 ixtends Person" (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!

JLS §9.1.3:

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 extending 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 also interface A extends B, and perhaps even class 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.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Thank you for the clarification. I wrote an email to my professor refering to the JLS § 9.1.3 you pointed out and asked him for an explanation. – linuxman Feb 20 '22 at 22:52
  • @linuxman Be prepared that the professor won't change their mind. This is [not the only time](https://stackoverflow.com/q/52264638/507738) such thing happens. – MC Emperor Feb 21 '22 at 07:06
  • Thanks for the hint. This is my prof's answer: "it uses the same keyword, but conceptually it's not an inheritance, it's an extension of the base interface. But what is true in both cases is that it is subtyping. However, subtyping is not the same as inheritance, since it can also be achieved through interface extensions." – linuxman Feb 21 '22 at 08:51
  • Is there anything I can add to that? I feel like my professor did not even look at the JLS §9.1.3. – linuxman Feb 21 '22 at 12:57
  • They are blind to their own error. They are still applying their personal understanding of terminology that is perhaps shared by a sizable chunk of the community but by no means an overwhelming majority, and no spec document exists. More crucially, they don't seem to think it's on them to show the work - sounds like they are of the opinion that their word is all you ever need to know. If you want to keep tugging at this thread, ask them for 'proof': Where does it say this stuff? If they refer to a book, ask: So where did the book's author get it from? Not the spec - see JLS 9.1.3. – rzwitserloot Feb 22 '22 at 11:02
0

Maybe to give an example of inheritance we should point to something from CrudRepository which is inherited by UserRepository.

clwhisk
  • 1,805
  • 1
  • 18
  • 17