tags:

views:

150

answers:

5

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
+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
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
Implementing many interfaces makes for wide inheritance trees, 'though, not for deep ones.
Joachim Sauer
Actually not - see @pinichi's answer.
Péter Török
@Joachim Sauer, true, I was just elaborating that you can both go infintely deep in inheritance and infinitely wide too....
The Elite Gentleman
@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
@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
+9  A: 

Read this (Its fun)

The system is out of resources.
Consult the following stack trace for details.
java.lang.StackOverflowError
pinichi
+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
Interesting experiment, but @Jonathans answer is still true: for all practical applications there is no limit.
Joachim Sauer
nice article...
org.life.java
+1 for the link. I ll take that as an answer :)
Rig Veda
A: 

There is no limit specified. For all practical purposes, it is infinite.

Ashish