Note that there are some explanatory texts on larger screens.

plurals
  1. PO"method ___() in ___ is defined in inaccessible class or interface" compilation error
    text
    copied!<p>I found a strange compilation restriction which I cannot explain, and I don't understand this restriction's reason.</p> <p><strong>Example-1:</strong></p> <p>Consider these classes: </p> <p>In <code>package e1;</code>:</p> <pre><code>public class C1 { enum E1 { A, B, C } public E1 x; } </code></pre> <p>In <code>package e2;</code>:</p> <pre><code>import e1.C1; public class C2 { public String test(C1 c1) { return c1.x.toString(); // here compilation error } } </code></pre> <p>This causes the following compilation error:</p> <blockquote> <p>Error:(5,20) java: <code>toString()</code> in <code>java.lang.Enum</code> is defined in an inaccessible class or interface</p> </blockquote> <p><strong>Example-2:</strong></p> <p>Consider these classes:</p> <p>In <code>package i1;</code>:</p> <pre><code>public interface I1 { int someMethod(); } public class C1 { static class I2 implements I1 { public int someMethod() { return 1; } } public I2 x = new I2(); } </code></pre> <p>In <code>package i2;</code>:</p> <pre><code>import i1.C1; import i1.I1; public class C2 { public static void main(String[] args) { C1 c1 = new C1(); System.out.println(c1.x.someMethod()); // compilation error } } </code></pre> <p>This also causes the same compilation error, but if we change the offending line to: </p> <pre><code>System.out.println(((I1)c1.x).someMethod()); </code></pre> <p>Then this can be compiled and works fine.</p> <hr> <p>So, the question is: </p> <p><strong>Why is this restriction of accessibility needed?</strong></p> <p><strong>Yes, I understand that classes <code>C1.E</code> in example-1) and <code>C1.I2</code> in example-2) are package private.</strong> But at the same time it's clear that nobody could assign weaker access privileges to methods of a base interface (<code>I1</code> of <code>Object</code>), so it will be always safe to make direct casting of object to its base interface and get access to restricted method.</p> <p>Could somebody explain the purposes and the reason of this restriction?</p> <p><strong>UPDATE</strong>: <a href="https://stackoverflow.com/users/829571/assylias">assylias</a> pointed out JLS <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6.1" rel="nofollow noreferrer">§6.6.1</a>:</p> <blockquote> <p>A member (class, interface, field, or method) of a reference (class, interface, or array) type or a constructor of a class type is accessible only if the type is accessible...</p> </blockquote> <p>Looks like this is the restriction, but it doesn't explain <strong>why</strong> this restriction (in the cases of the above mentioned examples) is needed...</p>
 

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