I was wondering if there is a finite number of time that I can inherit a class? Or what are the factors that can influence this?
+4
A:
For all practical applications, inheritance trees in Java may grow to infinite size.
Jonathan Grynspan
2010-10-15 08:59:19
+3
A:
There's no specific limit in the specification.
There's probably a implementation-dependent limit, if only in that you're limited by the available memory (class information needs to be stored somewhere after all).
And if you ever reach that limit, your design is broken anyway (anything that goes into the double digits is definitely a code smell).
Joachim Sauer
2010-10-15 08:59:38
A:
If a class is final
, you cannot inherit it.
Besides that, you can have infinitely inheritance trees. You can also infinitely implement
many interface classes too.
The Elite Gentleman
2010-10-15 09:00:58
Implementing many interfaces makes for wide inheritance trees, 'though, not for deep ones.
Joachim Sauer
2010-10-15 09:06:46
Actually not - see @pinichi's answer.
Péter Török
2010-10-15 09:07:27
@Joachim Sauer, true, I was just elaborating that you can both go infintely deep in inheritance and infinitely wide too....
The Elite Gentleman
2010-10-15 09:08:28
@The Elite Gentleman, again, neither is true. The same article states that inheritance width limit is (at most) 65535 - in practice it might be even less. Of course, none of us will ever get even close to these limits in real life projects - but still, there are limits.
Péter Török
2010-10-15 09:12:38
@Péter Török, I read the article, and you are indeed correct. "Practically" (in parenthesis), it is possible though. I wonder if I add 65536th class, would the compiler bomb me out?
The Elite Gentleman
2010-10-15 09:21:27
+9
A:
Read this (Its fun)
The system is out of resources. Consult the following stack trace for details. java.lang.StackOverflowError
pinichi
2010-10-15 09:01:30
+1 for some interesting research, answer being ~60 levels deep for actually compiling *and running* for those who want to know the answer before clicking the link
Brian
2010-10-15 09:05:47
Interesting experiment, but @Jonathans answer is still true: for all practical applications there is no limit.
Joachim Sauer
2010-10-15 09:11:34
A:
There is no limit specified. For all practical purposes, it is infinite.
Ashish
2010-10-15 09:02:08