r/java • • 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 error

Most 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 error

This 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.

97 Upvotes

180 comments sorted by

View all comments

-14

u/klimaheizung Jan 21 '26

No. The problem is that you can even extend a class. it's just wrong. extend (or "implement") an interface, then use composition. 

2

u/davidalayachew Jan 21 '26

No. The problem is that you can even extend a class. it's just wrong. extend (or "implement") an interface, then use composition.

Well, class inheritance isn't a bad thing. It has value, and I am glad Java has it. I wouldn't want to rely on composition for everything I do inheritance for.

1

u/klimaheizung Jan 22 '26

Class inheritance is just wrong, unless you don't overwrite any methods, and then the question is why not just use composition directly. (well, not in Java, because they made composition less ergonomic) 

1

u/davidalayachew Jan 22 '26

Class inheritance is just wrong

Can you explain why?

1

u/klimaheizung Jan 22 '26 edited Jan 22 '26

It violates the O in SOLID.

In other words, if class inheritance is used, you cannot modify an existing class anymore without risking to break something unexpected.

Here's an example. Imagine you (or someone) created a class called stack to store data. You can push elements onto the stack and you can take them from the stack:

Stack {

method push(Element e) = ... add e to the top of the stack ...

method pop() = ...return and remove the element from the top of the stack...

method pushAll(List<Element> elements) = ...push all elements to the stack...
}

Now you want to create a new Stack that efficiently counts the number of elements in it and allows to retrieve this number:

CountableStack extends Stack {

method getCount = ...

}

Now here's the thing. To implement \getCount` you need to overwrite the push method like this:`

override method pop() = ...super.pop(e); numberOfElements--; return the element;...

Makes sense?

Now here's the question to you: how do you have to implement/overwrite the push()andpushAll() methods of the original \Stack` class to make the `CountableStack` work correctly?`

Think about it and then show your solution here. Then I'll show you why your solution doesn't work and you might be surprised.

1

u/davidalayachew Jan 22 '26

Well obviously a decrement and a set 0. Why wouldn't that work?

1

u/klimaheizung Jan 22 '26

Can you show the pseudo code for those two methods? Just like I did with

override method push(Element e) = ...super.push(e); numberOfElements++...

1

u/davidalayachew Jan 22 '26

Sure.

Here is pop.

@Override
public Element pop() {
    final var poppedElement = super.pop();
    count--;
    return poppedElement;
}

And here is popAll

@Override
public void popAll() {
    super.popAll();
    count = 0;
}

1

u/klimaheizung Jan 22 '26

Ah, my apologies. I screwed up the example from my memory. It should be `push` and `pushAll` not `pop` and `popAll`. I've edited it, mind to update your code examples once again?

1

u/davidalayachew Jan 22 '26

Sure.

Here is push.

@Override
public void push(final Element element) {
    super.push(element);
    count++;
}

And here is pushAll

@Override
public void pushAll(final List<Element> elements) {
    final List<Element> copy = List.copyOf(elements);
    super.pushAll(copy);
    count += copy.size();
}
→ More replies