Reputation: 6325
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?
Upvotes: 1
Views: 726
Reputation: 60424
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:
A B
\ /
C
But it's actually like this:
A
|
B
|
C
The most obvious difference is that changes to A
do not affect instances of B
in the first graph, but would impact B
in the second.
Upvotes: 6
Reputation: 2886
A class can extend one class that can extend another class. But one class can't inherit two classes. It will cause ambiguity. That's why java don't support multiple inheritance.
MyException extending Exception which is extending Throwable. That is the chain of inheritance. It won't cause ambiguity.
Upvotes: 0
Reputation: 24336
So in your example MyException
is also a Throwable
in addition to being an Exception
. Therefore the hierarchy is like so:
Throwable -> Exception -> MyException
So lets say that the hierarchy actually looked like this:
Throwable -> MyException
Exception -> MyException
While still true that MyException
is both a Throwable
and an Exception
. For an explanation as to what multiple inheritance brings look here:
"In object-oriented programming languages with multiple inheritance, the diamond problem (sometimes referred to as the "deadly diamond of death"1) is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If D calls a method defined in A (and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B, or C?"
Upvotes: 0
Reputation: 114
It is a chain of inheritance
Object -> Throwable -> Exception -> MyException
See here http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
Upvotes: 0
Reputation: 62469
I suppose this is done the same way that every class extends Object but can also be subclassed once?
Not quite. Think of it this way:
class A { }
can be thought of as
class A extends Object { }
but
class A extends B { }
should be though of as
class B extends Object { }
class A extends B { }
and not
class A extends B, Object { }
In the class inheritance hierarchy (not counting implementations of interfaces) each class can have a single parent, except for Object
which the root of the tree.
Upvotes: 0