4

Possible Duplicate:
Reduce visibility when implementing interface in Java

I must be missing something obvious, but i am getting:

Cannot reduce the visibility of the inherited method

And I don't see how. This is my interface:

interface QueryBuilderPart {
    StringBuilder toStringBuilder();
}

And this is my implementation:

public class Stupid implements QueryBuilderPart {
    @Override
    StringBuilder toStringBuilder() {
        return null;
    }
}

Both the class and the implementation are in the same package. Any Ideas?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lucas
  • 14,227
  • 9
  • 74
  • 124

1 Answers1

14

By default interface's method is public, but you reduce it to default visibility, which is package level visibility.

So the following two block of code are the same:

interface QueryBuilderPart {
    StringBuilder toStringBuilder();
}

interface QueryBuilderPart {
    public abstract StringBuilder toStringBuilder();
}

Note that interface's method is abstract as well

So you should do as following:

public class Stupid implements QueryBuilderPart {
    @Override
    public StringBuilder toStringBuilder() {
        return null;
    }
}
Pau Kiat Wee
  • 9,485
  • 42
  • 40
  • Not just by default - every method declared by an interface is implicitly public, even if it's not explicitly declared as such. To get a package-level abstract method, you'll have to use an abstract class, not an interface. – Alex Apr 19 '12 at 16:22