Search code examples
javainterfacevisibilityimplements

I am not reducing visibility from interface yet I get the "Cannot reduce visibility" compile error


I am working on a employee management system for training, and I made some interfaces so I can work from there.
As I was working on it, one of my methods gave me a compiler error, saying that I could not reduce visibility. But I never did.
Here is the interface and the method that gives me this problem.
Interface:

interface setter{
    boolean setPerformance(int id,int score);
    void setAttendance();
    boolean setNewEmployee(int id,String passWord,String name);
    boolean setRank(int id,int rank);
}

The class with the method :

class localSetter implements setter{

    localGetter get;
    browser browse;

    boolean setPerformance(int id,int score) {
        if(get.getRank(browse.returnBrowserId()) >= 1 || Integer.toString(score).length() > 2) {
            return false;
        } else {
            String oldValue = localGetter.storage.get(id);
            String newValue = oldValue.replaceFirst("Pf:\\d\\d", "Pf:"+score);
            localGetter.storage.put(id,newValue);
            return true;
        }
    }

    void setAttendance() {
        String oldValue = localGetter.storage.get(browse.returnBrowserId());
        int start = oldValue.indexOf("At:");
        String newValue = oldValue.substring(0,start + 3) + browse.currentDate() + oldValue.substring(start + 3);
        localGetter.storage.put(browse.returnBrowserId(),newValue);
    }

    boolean setNewEmployee(int id,String passWord,String name) {
        if(get.getRank(browse.returnBrowserId()) > 2) {
            String value = "Ps:"  + passWord + "Nm:" + name + "Pf:0";
            localGetter.storage.put(id, value);
            return true;
        } else {
            return false;
        }
    }
    boolean setRank(int id, int rank) {
        if(localGetter.storage.isEmpty() || get.getRank(browse.returnBrowserId()) >= 3) {
            if(localGetter.storage.containsKey(id) == false) {
                return false;
            } else {
                String oldValue = localGetter.storage.get(id);
                String newValue = oldValue.replaceAll("Rk:\\d+","Rk:" + rank);
                localGetter.storage.put(id, newValue);
                return true;
            }
        } else {
            return false;
        }
    }

As you can see, both the interface and the implemented method has no visibility modifiers, yet it keeps telling me to add the "public" modifier.
Somebody help me please. Thanks.


Solution

  • Methods in interfaces are implicitly public. In classes, if you don't specify an access modifier, the method would be accessible only by classes from the same package, thus reducing the visibility of the method.

    TL;DR - a method implementing an interface's method, must be public.