Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From Java Language Specification:</p> <p><a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8.3" rel="nofollow noreferrer">jls-8.4.8.3</a></p> <blockquote> <p>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:</p> <ul> <li>If the overridden or hidden method is public, then the overriding or hiding method must be public; otherwise, a compile-time error occurs.</li> <li>...</li> </ul> </blockquote> <p>Notice that you are trying to override <code>public</code> method <code>getNum()</code> inherited from class <code>B</code> (and also from interface <code>A</code>) with new one that has <code>protected</code> access modifier. It means that you are trying to reduce visibility of this method which according to specification is incorrect.<br> To be able to override this method you need to use <code>public</code> access modifier with your new version of that method.</p> <hr> <p>Why you cant reduce visibility? Take a look at below code which uses your classes but is placed inside some other package and ask yourself "how should this code behave?".</p> <pre><code>package my.pckage; import your.pckage.A; import your.pckage.C; public class Test{ public static void main (String[] args){ C C = new C(); c.getNum();// ERROR: Test class doesn't have access to `c`s protected method. // Why should it have, Test doesn't extend C. A a = (A)c;// Lets try using other reference a.getNum();// Should `a` have access to method that is protected in `C`? // If yes, then what is the point of declaring this method // protected if all I would have to do to get access to it is // casting instance of C to A interface? } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload