Access Modifiers in Scala Last Updated : 26 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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: Private Protected Public Table: Modifier Class Companion Subclass Package World No Modifier/Public Yes Yes Yes Yes Yes Protected Yes Yes Yes No * No Private Yes Yes No No * No 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. Example: Scala // Scala program of private access modifier class abc { private var a:Int = 123 def display() { a = 8 println(a) } } object access extends App { // class abc is accessible // because this is in the same enclosing scope var e = new abc() e.display() } Output: 8 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. Example: Scala // Scala program of protected access modifier class gfg { // declaration of protected member protected var a:Int = 123 def display() { a = 8 println(a) } } // class new1 extends by class gfg class new1 extends gfg { def display1() { a = 9 println(a) } } object access extends App { // class abc is accessible because this // is in the same enclosing scope var e = new gfg() e.display() var e1 = new new1() e1.display1() } Output: 8 9 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 // Scala program of protected access modifier class gfg { var a:Int = 123 } object access extends App { var e = new gfg() e.a = 444 println(e.a) } Output: 444 Comment More infoAdvertise with us Next Article Access Modifiers in Scala S schrodinger_19 Follow Improve Article Tags : Python Practice Tags : python Similar Reads How to access list elements in Scala? In this articleIn this article, we will learn to access list elements in Scala. List in Scala is the most commonly used collection type, providing an ordered collection that allows accessing elements by index or using functional programming. How to Access List Elements in Scala?Below are the possibl 2 min read Overriding Accessors and Mutators in Scala A standardized way to override default Accessors and Mutators hasn't been fixed by Scala, yet users of this programming language use one specific and a common method for overriding which is discussed in detail below. Before we get started, what are Accessors and Mutators? Accessors and Mutators sim 3 min read Parameterless Method in Scala Prerequisites - Scala | Functions A parameterless method is a function that does not take parameters, defined by the absence of any empty parenthesis. Invocation of a paramaterless function should be done without parenthesis. This enables the change of def to val without any change in the client cod 2 min read ListMap in Scala Immutable maps Implemented by using a list-based data structure. The Scala List class holds a sequenced, linear list of items. We must import scala.collection.mutable.ListMap for ListMap. ListMap collection used only for a small number of elements.Syntax: var listMapName = ListMap("k1"->"v1", "k2 3 min read Interesting fact about Scala Scala(pronounced as "skah-lah") is general-purpose programming language designed by Martin Odersky. The design of Scala started in 2001 at EPFL, Lausanne, Switzerland. Scala was released publicly in 2004 on Java platform. Scala is designed to be concise and addresses criticisms of Java. Scala source 3 min read Like