uml
uml

Reputation: 1189

Compilation failing

This is a snippet of Java code:

interface Rideable {
    String getGait();
}

public class Camel implements Rideable {
    int x = 2;
    public static void main(String[] args) {
        new Camel().go(8);
    }

    void go(int speed) {
        System.out.println((++speed * x++) + this.getGait());
    }

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

It turns out that compilation fails (according to Oracle) though in my opinion it shall run fine producing output. So, where is the culprit for failing compilation? Cheers

Upvotes: 2

Views: 161

Answers (2)

Reimeus
Reimeus

Reputation: 159754

You have defined getGait with default access but interface definitions are require their implementations to be public.

public String getGait() {

Upvotes: 2

arshajii
arshajii

Reputation: 129487

You can't reduce the visibility of methods you override (interface methods are public by default), try this instead:

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

Upvotes: 6

Related Questions