I'm almost certain this question has been asked, but I'm not sure what to search for regarding it.
Anyway, I was was curious if it would be possible to create a class that extends ByteBuffer
. I thought it would be impossible due to ByteBuffer
having package-private constructors:
// package-private
ByteBuffer(int mark, int pos, int lim, int cap, byte[] hb, int offset) {
super(mark, pos, lim, cap);
this.hb = hb;
this.offset = offset;
}
// Creates a new buffer with the given mark, position, limit, and capacity
//
ByteBuffer(int mark, int pos, int lim, int cap) { // package-private
this(mark, pos, lim, cap, null, 0);
}
However, I found out if you create your class in a package that shares a name with its parent, then it compiles perfectly.
package java.nio;
public class Test extends ByteBuffer {
Test(int mark, int pos, int lim, int cap, byte[] hb, int offset) {
super(mark, pos, lim, cap, hb, offset);
}
@Override
public ByteBuffer slice() {
return null;
}
...
}
It compiles in Java 9 and Java 10 as well, but only when using --patch-module
when compiling:
javac --patch-module java.base=. java/nio/Test.java
My question is: How (and why) does this compile?