1
interface Rideable { 
    String getGait(); 
}


public class Test implements Rideable {
    int weight = 2;

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

    void go(int speed) {
        ++speed; weight++;
        int walkrate = speed * weight;
        System.out.print(walkrate + getGait());
    }
    public static void main(String[] args) {
        new Test().go(8);
    }
}

Why it showing the above error?

While running this I get compilation fails

What is the access specifier of the method getGait() in the Test class?

gprathour
  • 14,813
  • 5
  • 66
  • 90

2 Answers2

4

The methods declared in interface are by default public.

And in overriding we can only increase the scope, we cannot reduce it.

In the class where you are overriding the method, you have not mentioned public, so by default it considers it to be default (package private). So it is trying to reduce the scope and as a result you get that error.

You need to do the following,

@Override
public String getGait(){

}
gprathour
  • 14,813
  • 5
  • 66
  • 90
0

When you extend an interface that contains a default method, you can do the following:

  • Not mention the default method at all, which lets your extended interface inherit the default method.

  • Redeclare the default method, which makes it abstract.

  • Redefine the default method, which overrides it.

You are trying to do the last one (i.e. method overriding), but to successfully do that you must keep the method signature same. Just add access specifier "public":

public String getGait() {
    return " mph, lope"; 
}
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79