3

since all interface method are public, does that mean all implementation of interface method must be public as well? I have read that subclass can only assign access level that is higher than the original method. I have tried this with some code and it does seem to be true, but I was just wondering where can I find documentation regarding to this?

Thor
  • 9,638
  • 15
  • 62
  • 137
  • Yes, that's true. Have a look at [this answer](http://stackoverflow.com/a/36422728/276052) from earlier this week. – aioobe Apr 10 '16 at 09:16
  • 2
    You seem to have all you need. Note: in Java 9, private methods on interfaces will be allowed. – Peter Lawrey Apr 10 '16 at 09:16
  • 3
    I think OP is asking for the *documentation*. – Maroun Apr 10 '16 at 09:21
  • 2
    @TonyStark can you change the title of your question to highlight that you are looking for the _documentation_, not an _explanation_? Some people seem not to be reading the actual question. – Modus Tollens Apr 10 '16 at 09:25
  • @PeterLawrey do you please tell me how you got to know that java 9 will allow private on interface? is there an website where I can find the latest news regarding to java? – Thor Apr 10 '16 at 09:28
  • 1
    @TonyStark take a look at http://mail.openjdk.java.net/pipermail/jdk9-dev/2015-March/001981.html and http://aruld.info/private-interface-methods-in-java-9/ regarding private methods in Java 9 – Modus Tollens Apr 10 '16 at 11:53
  • 1
    Looks like no one cares that this question is off-topic, since it requests off-site resources ... – Tom Apr 10 '16 at 11:57
  • @Tom You are right. Note to self: Don't post when tired! Flagged. – Modus Tollens Apr 10 '16 at 12:42

3 Answers3

4

You can find the official documentation of that requirement in the Java Language Specification.

Quoting from 8.4.8.3. Requirements in Overriding and Hiding:

The access modifier (§6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method, as follows:

If the overridden or hidden method is public, then the overriding or hiding method must be public; otherwise, a compile-time error occurs.

If the overridden or hidden method is protected, then the overriding or hiding method must be protected or public; otherwise, a compile-time error occurs.

If the overridden or hidden method has package access, then the overriding or hiding method must not be private; otherwise, a compile-time error occurs.

Modus Tollens
  • 5,083
  • 3
  • 38
  • 46
3

As in the official Java Tutorial by Oracle:

interfaces are a kind of contract all implementing classes have to stick to

This means, the signature of a method, the return type and the access modifier are not allowed to be changed.

https://docs.oracle.com/javase/tutorial/java/IandI/index.html

Also if you try to tag an interface-method with the @Override annotation, the compiler will throw an error.

hallucinations
  • 3,424
  • 2
  • 16
  • 23
Wecherowski
  • 818
  • 11
  • 24
1

Interfaces are created to be an interface for using classes that implement them.

For example we implement a Comparable interface to be trusted that the class has compareTo method which can be used by other classes.

So it's not possible and I don't see a point in doing that.

I didn't found a part of documentation which exactly refers to your question but it can be understood from this part:

If the overridden or hidden method is public, then the overriding or hiding method must be public; otherwise, a compile-time error occurs.

Ali Seyedi
  • 1,758
  • 1
  • 19
  • 24