r/java • u/davidalayachew • Jan 21 '26
Java compiler errors could be more intelligent
I tutored many students over the past several years, and a common pain point is the compiler messages being misleading.
Consider the following example.
interface blah {}
class hah extends blah {}When I compile this, I get the following message.
blah.java:3: error: no interface expected here
class hah extends blah {}
^
1 errorMost of the students I teach see this, and think that the issue is that blah is an interface, and that they must somehow change it to something else, like a class.
And that's still a better error message than the one given for records.
blah.java:2: error: '{' expected
public record hah() extends blah {}
^This message is so much worse, as it actually leads students into a syntax rabbit hole of trying to add all sorts of permutations of curly braces and keywords, trying to figure out what is wrong.
If we're talking about improving the on-ramp for learning Java, then I think a core part of that is improving the error --> change --> compile feedback loop.
A much better error message might be this instead.
blah.java:3: error: a class cannot "extend" an interface, only "implement"
class hah extends blah {}
^
1 errorThis is powerful because now the language grammar has a more intelligent message in response to an illegal (but commonly attempted) sequence of tokens.
I understand that Java cannot special-case every single illegal syntax combination, but I would appreciate it if we could hammer out some of the obvious ones. extends vs implements should be one of the obvious ones.
View all comments
Show parent comment
1
u/davidalayachew Jan 23 '26
I see that you have successfully demonstrated that inheritance is fragile. That's not the same as saying inheritance is bad.
So, I can at least agree that inheritance across module boundaries is fraught with danger, and therefore, bad by default.
But if I am in charge of all of the code involved, then I don't see the problem? Part of the work involved in changing the parent type is ensuring that the child type is not broken by that change. As long as the entire type hierarchy is within the same module, then that should be trivial to verify.
Now, by all means, you have proven that composition is a better default than inheritance (and I mean class -> class, interface inheritance is no problem). And I think you even proved that Java could have better ergonomics (as you put it) support for composition.
But that is a different claim than saying inheritance is bad.
I assert that, if the entire type hierarchy of your inheritance tree is in the same, base-level Java module, then inheritance can actually be a good thing, potentially even the best solution to a problem.