I understand that the language specification declares only single inheritance, but I’m puzzled over how this one example seems to break the rule.
If I write an Exception class, I’ll write:
public class MyException extends Exception {
//class body
}
Only, if you look here, you’ll notice that the class Exception extends Throwable.
So to my mind, we have (the theoretical code example)
public class MyException extends Throwable, Exception {
//class body
}
Why is this not so?
I suppose this is done the same way that every class extends Object but can also be subclassed once?
There can be multiple levels in the hierarchy. A class may extend a class that itself extends another class (or implements an interface). This is not the same (in theory or in practice) as one class extending two classes itself.
Interfaces in Java do provide much of the same functionality as multiple inheritance in other languages (because a class can implement multiple interfaces), but that’s not what you’re seeing here. This is standard single inheritance with multiple ancestor classes.
It might help to picture a (biologically weird) family tree in which every child has exactly one parent. Those children may also have a grandparent, but never more than one node at any level of the structure.
You’re imagining a structure that looks like this:
But it’s actually like this:
The most obvious difference is that changes to
Ado not affect instances ofBin the first graph, but would impactBin the second.