7

I have two files:

public interface PrintService {
    void print(PrintDetails details);
    class PrintDetails {
        private String printTemplate;
    }
    public interface Task {
        String ACTION = "print";
    }
}

and

public class A implements PrintService {
    void print(PrintDetails details) {
        System.out.println("printing: " + details);
    }
    String action = PrintService.Task.ACTION;   
}

I thought the code looks okay, but I am getting an error in the second file for the line void print(PrintDetails details) { that states:

Cannot reduce the visibility of the inherited method from PrintService.

Can someone explain what this means for me?

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
Alan2
  • 23,493
  • 79
  • 256
  • 450

2 Answers2

24

In a Java interface each method is by default public:

Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.

Every method declaration in the body of an interface is implicitly public. [..]

In an implementing class you are not allowed to reduce the visibility, and by not specifying an access modifier:

void print(){..}

you are specifying the access level default, which has lower visibility than public.

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
3

Make methode public in the class in which interface implement,because in interface by default every method is public and abstract.

willome
  • 3,062
  • 19
  • 32
Ravi
  • 31
  • 1