-2
interface Rideable {

    String getGait();

}

public class Camel implements Rideable {

    String getGait() { return " mph, lope"; }

}

Why does the compilation fail? I really don't know why the compile error?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
ilakk
  • 69
  • 1
  • 8
  • what do the compilation errors tell you? – Reimeus Apr 19 '16 at 20:37
  • Did you read the error? – SLaks Apr 19 '16 at 20:38
  • Interface methods are public by default. You're trying to implement the method with a package private method (which is more restrictive). See [this chart](http://stackoverflow.com/a/33627846/276052) for a comparison between public and package private. – aioobe Apr 19 '16 at 21:01

2 Answers2

2

By default the modifier for interface's method is public. So when you implement it. It need to be public. Add public to your getGait method should resolve it

Tuan
  • 34
  • 1
1

Methods in interface are by default public and abstract and data member are by default public, static and final as access label. In subclass implementation, you have not mentioned any modifier. So it will have default access. While giving the implementation you can't restrict(provide lesser) the access label. So you have to provide public as access modifier while implementing the methods of interface.

RCS
  • 1,370
  • 11
  • 27