4 ms·
Ask YC: An analysis of Java's Class Library - any ideas on what it means?
For a while I've been working on an opinion piece on Object Orientation for my company blog (http://www.jodoro.com).
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.
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.
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.
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.
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).
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.
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.
Any before I went off on any crazy tangents, I wanted to ask YC/HN what they thought of all this?
- noodle 18y agoi 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 18y agoThat 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...
- jacobscott 18y agoWhat 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?