In Java, it is possible to create package-private interfaces. Looking at them with javap, you see that they lack the "public" visibility.
In Scala, you can declare a trait as private[package] or protected[package], but looking at in with javap, it is still public.
So how do you create a package-private trait in Scala?
While the Scala compiler respects the visibility, my problem is that my API will probably be accessed from Java too, and I don't want to expose my internal implementation to Java.
In scala, trait is a collection of abstract and non-abstract methods. You can create trait that can have all abstract methods or some abstract and some non-abstract methods. A variable that is declared either by using val or var keyword in a trait get internally implemented in the class that implements the trait.
In Scala,private[this] is object private,which makes sure that any other object of same class is unable to access private[this] members.
In Scala, one trait can inherit another trait by using a extends keyword. Traits support multiple inheritance. In Scala, a class can inherit both normal classes or abstract class and traits by using extends keyword before the class name and with keyword before the trait's name.
A Trait is a concept pre-dominantly used in object-oriented programming, which can extend the functionality of a class using a set of methods. Traits are similar in spirit to interfaces in Java programming language. Unlike a class, Scala traits cannot be instantiated and have no arguments or parameters.
I believe this is the answer to your question
http://www.scala-lang.org/node/10488
private has a subtly special status in the language specs of both Scala and Java. Check out the discussion of private vs qualified private in the Modifiers section of the SLS. In short, private is the same as Java private, whereas private[foo] is not marked private in the bytecode, but simply involves a compile-time access check.
I don't believe you can truly make a trait package private once it is compiled into bytecode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With