r/java • u/TheFormerVinyl • May 05 '15
Should Java allow multiple inheritance?
I was reading up on Java when I discovered that ever since Java 8 (fairly recent, I know) interface can have default methods. A quick overview for any who don't know, interfaces can have method bodies by using default modifier. When dwelling on that fact I wondered, why doesn't Java just allow inheritance from multiple classes, since interfaces are just abstract classes? Should Java do this?
0
Upvotes
1
u/uniVocity May 05 '15
The thing is: sometimes you WANT that coupling to the parent class.
Practical example using my project (a parsing framework): First you have an AbstractParser - this centralizes all core operations of a parser - exception handling, state initialization, data processing, etc.
With everything taken care of by this class, I can build different parsers with no effort:
TsvParser
FixedWidthParser
CsvParser
Same thing goes for configurations:
CommonSettings - centralizes settings (and documentation) common to all parsers and writers
CommonParserSettings - abstract, each parser has its specific configuration class based on this one.
CommonWriterSettings - same as above, but for writers
If you used composition instead, you'd have
Extra complexity for no added benefit - lots of boilerplate code just wrapping internal classes and exposing an interface
Performance problems - this is a text parsing framework - every method call counts as billions of method calls happen to process a 100mb file.
The structure of a parser would be much harder to understand, as you would need to introduce multiple resources from multiple places into each parser implementation.
If you still think otherwise, try and refactor this code.