Created
February 2, 2016 18:48
-
-
Save travisbrown/b9c01e0f40e05cf0427d to your computer and use it in GitHub Desktop.
Package private types in your public API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/main/java/j/bar/Bar.java | |
package j.bar; | |
abstract class Foo { | |
public abstract String value(); | |
} | |
public class Bar { | |
public static Foo makeFoo() { | |
return new Foo() { | |
public String value() { | |
return "whatever"; | |
} | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/main/java/j/CallJava.java | |
package j; | |
public class CallJava { | |
public static void main(String[] args) { | |
/** | |
* The following line would fail with: | |
* j/qux/Qux.java:5: error: value() in Foo is defined in an inaccessible class or interface | |
* System.out.println(j.bar.Bar.makeFoo().value()); | |
* ^ | |
*/ | |
//System.out.println(j.bar.Bar.makeFoo().value()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/main/java/j/CallScala.java | |
package j; | |
public class CallScala { | |
public static void main(String[] args) { | |
System.out.println(s.bar.Bar.makeFoo().value()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/main/scala/Demo.scala | |
package s | |
object CallScala { | |
def main(args: Array[String]): Unit = println(s.bar.Bar.makeFoo.value) | |
} | |
object CallJava { | |
def main(args: Array[String]): Unit = println(j.bar.Bar.makeFoo.value) | |
} | |
package bar { | |
private[bar] abstract class Foo { | |
def value: String | |
} | |
object Bar { | |
def makeFoo: Foo = new Foo { | |
def value = "whatever" | |
} | |
} | |
} |
Author
travisbrown
commented
Feb 2, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment