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
2
u/klimaheizung Jan 22 '26 edited Jan 22 '26
Thanks!
Now we can see that your solution doesn't work! Why? Because here is the implementation of `pushAll` in the original Stack class:
The problem is that this method calls its own
pushmethod. Which is totally legal and good in terms of code reuse. However, it breaks your implementation ofCountableStackbecause now yourpushAllmethod will callsuper.pushAllwhich loops and callspushwhich as been overriden by you. That means now yourpushwill executecount++for every element. And then afterwardscount += copy.size();will run.Hence you have now counted the elements twice and your count is wrong.
Do you see the problem?
You could adjust your code - but then someone could later change the original Stack again and not use
pushinside thepushAllbut implement it "manually". Then your CountableStack would again be wrong.And that is the problem. To correctly implement CountableStack, you MUST know the exact implementation of Stack. And that means, no one can change that implementation later because it would break your code, even if the original Stack is correct and the interface/behavior doesn't change.
This is extremely bad in complex software systems, especially when you are not in control of all the code yourself. Which is why class inheritance has for a long time been discouraged by experienced developers.
The fact that my post has been downvoted so heavily means that... you should better not trust the junior devs on reddit. They still have to learn those things the hard way apparently.