

Ask YC: An analysis of Java's Class Library - any ideas on what it means?  - jwilliams

For a while I've been working on an opinion piece on Object Orientation for my company blog (http://www.jodoro.com).<p>I decided to try and generate a few facts and figures to flesh my article out. With that in mind, I did a quick analysis of the Java Class Library (JDK1.5 to be precise). What I found was curious, so I decided to share some of the raw figures whilst I mulled over my article.<p>If you take a look at the class library for Java, you'll find that the majority of the libraries either inherit directly from java.lang.Object, or are just one level of separation away (In fact this 1,2 level is ~60% of the class library). If you take the Swing package out of the equation, almost half the library (47%) directly inherits from Object.<p>The maximum level of inheritance depth is 7. This depth amounts to less than half a percent of the total. Perhaps not surprising to anyone who has worked with Swing, but this maximum level only occurs in the javax.swing package. It includes deeply nested things like javax.swing.JCheckBoxMenuItem.AccessibleJCheckBoxMenuItem. Moving upwards - Swing is also 65% of the 6th level of depth, with java.util being the other majority. Swing also represents 25% of the 5th level of depth. Again, if you're familiar with Java and Swing this is not a big surprise.<p>Exceptions take up around 13% of the class definitions. If you take exceptions out of the previous examples, the figures go up. 48% of non-exception classes are at level 1, and 73% are at level 2 or below. Excluding Swing again these figures leap to 57% and 80% respectively.<p>Turning to interfaces. Interfaces only nest to a level of 4 - and 70% of all interfaces are either root (34%) or one level of extension (36%). Of the roughly 2,700 classes that ship with JDK 1.5, around 40% of them implement interfaces. Interestingly, this is reasonably consistent across most packages. Even quite hefty packages such as AWT and Swing clock in at about 45% each. Aside from outliers, most packages fall in the range of 30% to 50% (classes that implement interfaces).<p>Some other facts - Around 20% of classes are what I'd call "The most basic" - both at depth 1 (directly inherit from Object) and do not inherit from any interfaces. In other words, of the 60% of classes that don't have an interface, a third also do not inherit from anything aside from Object.<p>I've got a dozen theories on this - Some controversial, most mundane. The most logical conclusion to leap to is that the JDK is made up of a lot of concepts that come from systems programming, and aren't necessarily well suited to OO. However, right now I'm mostly just curious rather than particular on an agenda or soapbox.<p>Any before I went off on any crazy tangents, I wanted to ask YC/HN what they thought of all this?
======
jacobscott
What would be interesting is to slurp a bunch of OSS java and see how people's
use of classes (either by imports, or by method calls) correlates to
implementing an interface and/or not inheriting directly from Object. Maybe a
bunch of stuff is legacy?

------
noodle
i just think that as it developed from the hot new thing to the ubiquitous
language, things got tacked on and wrenched in just to get the functionality
in there.

~~~
jwilliams
That might be borne out by the stats - these figures can vary greatly between
packages. For example, Swing is quite significant.

However, that implies that these figures would be higher (greater levels of
abstraction) if things had been designed holistically...

