Default Access Modifier
Scala’s access modifier is different from Java:
-
Java defaults to package internal visibility if you don’t specify any access modifier. Scala, on the other hand, defaults to public.
-
Java provides an all-or-nothing proposition. Either it’s visible to all classes in the current package or it’s not visible to any. Scala gives fine-grained control over visibility.
-
Java’s protected is generous. It includes derived classes in any package plus any class in the current package. Scala’s protected is akin to C++ and C#—only derived classes can access it. However, you can also ask Scala for quite a liberal and flexible interpretation of protected.
-
Finally, Java encapsulation is at the class level. You can access the private
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access