Access Modifiers in scala are used to define the access field of members of packages, classes or objects in scala.For using an access modifier, you must include its keyword in the definition of members of package, class or object.These modifiers will restrict accesses to the members to specific regions of code.
There are Three types of access modifiers available in Scala:
What is companion in above table? It is a singleton object named same as the class.
1. Private: When a member is declared as private, we can only use it inside defining class or through one of its objects.
Here we declared a variable ‘a’ private and now it can be accessed only inside it’s defining class or through classes object.
2. Protected: They can be only accessible from sub classes of the base class in which the member has been defined.
When we extended abc in class new1, protected variable a is now available to be modified cause new1 is a subclass of class abc.
3. Public: There is no public keyword in Scala. The default access level (when no modifier is specified) corresponds to Java’s public access level.We can access these anywhere.
- Scala | Uniform Access Principle
- Scala Tutorial – Learn Scala with Step By Step Guide
- Set in Scala | Set-2
- Scala | Either
- Set in Scala | Set-1
- Scala Map
- Scala Byte %(x: Int): Int
- Scala | Equals
- Scala Byte /(x: Int): Int
- Scala short /(x: Int): Int
- Scala map contains() method with example
- Scala | Value Classes
- Packages In Scala
- Scala this keyword
- Scala For Comprehensions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.