Labels

Thursday, April 14, 2022

Access Modifiers in Scala (Class -38)

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 3 types of Access Modifiers in Scala:

a)      Public

b)      Private

c)       Protected


a)      Public: If nothing is mentioned before modifier, then it will automatically by default take as a Public. In other words, every member not labelled private or protected is public. There is no explicit modifier for public members. Such members can be accessed from anywhere.


b) Private: This is Case Class which has Companion Object and Companion Class. A private member is visible only inside the class or object that contains the member definition.

            If we want to import the schema into a data is called ‘Case Class’. This will create immutable data.


Case Class example:


c)  A Protected member is only accessible from subclasses of the class in which the member is defined. The availability of the member function is limited to the same class and its subclass. Excess without inheritance is treated as an error.

                                              protected def function_name(){}

No comments:

Post a Comment