Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, method scope can be controlled using access modifiers. These modifiers restrict the visibility and access of the defined methods. Here's a quick overview of the most commonly used access modifiers:
Public: This is the default access level if no modifier is provided. The method is accessible from any other code.
def publicMethod(): Unit = { println("This is a public method.") }
Private: The method is only accessible within the class (or object) it is defined in.
private def privateMethod(): Unit = { println("This is a private method.") }
Protected: The method is accessible within the class it's defined in and its subclasses.
protected def protectedMethod(): Unit = { println("This is a protected method.") }
Private[this]: This is a stricter version of private
. It limits access to only the specific instance of the class in which the method is defined. Other instances of the same class won't be able to call the method.
private[this] def veryPrivateMethod(): Unit = { println("This method is accessible only from this specific instance.") }
Package-specific: You can also limit access to methods such that they can be accessed only from within the same package.
private[package_name] def packageMethod(): Unit = { println("This method is accessible within the specified package.") }
Let's consider the following example:
package mypackage class MyClass { def publicMethod(): Unit = println("Public method") private def privateMethod(): Unit = println("Private method") protected def protectedMethod(): Unit = println("Protected method") private[this] def veryPrivateMethod(): Unit = println("Very Private method") private[mypackage] def packageMethod(): Unit = println("Package-specific method") } class SubClass extends MyClass { def testProtectedMethod(): Unit = { protectedMethod() } }
In the above example:
publicMethod
can be accessed from anywhere.privateMethod
can be accessed only within MyClass
.protectedMethod
can be accessed within MyClass
and its subclasses (as demonstrated in SubClass
).veryPrivateMethod
can be accessed only from the specific instance of MyClass
it is defined in.packageMethod
can be accessed by any class or object within the mypackage
package.Use these access modifiers judiciously based on the desired encapsulation level for your methods.
Controlling method visibility in Scala:
class MyClass { private def privateMethod(): Unit = { // Implementation } def publicMethod(): Unit = { privateMethod() // Implementation } }
Access modifiers for methods in Scala:
private
, protected
, and public
(default) to control method visibility.class AccessModifierExample { private def privateMethod(): Unit = { // Implementation } protected def protectedMethod(): Unit = { // Implementation } def publicMethod(): Unit = { // Implementation } }
Scala private and protected methods:
private
methods are accessible only within the defining class or object. protected
methods are accessible within the defining class, subclasses, and same-package classes.class ExampleClass { private def privateMethod(): Unit = { // Implementation } protected def protectedMethod(): Unit = { // Implementation } }
Package-private methods in Scala:
package com.example.mypackage class PackagePrivateExample { def packagePrivateMethod(): Unit = { // Implementation } }
Scala method visibility within classes and objects:
class Container { private def internalMethod(): Unit = { // Implementation } def publicMethod(): Unit = { internalMethod() // Implementation } }
Controlling method scope in Scala traits:
trait ExampleTrait { private def privateTraitMethod(): Unit = { // Implementation } def publicTraitMethod(): Unit = { privateTraitMethod() // Implementation } }
Method scope and inheritance in Scala:
class BaseClass { protected def protectedMethod(): Unit = { // Implementation } } class SubClass extends BaseClass { def useProtectedMethod(): Unit = { protectedMethod() // Implementation } }
Using implicit modifiers for method visibility in Scala:
public
visibility for methods. It is the default visibility level.class ExampleClass { def publicMethod(): Unit = { // Implementation } }
Method overriding and scope in Scala:
class BaseClass { protected def protectedMethod(): Unit = { // Implementation } } class SubClass extends BaseClass { override def protectedMethod(): Unit = { // Implementation } }
Method scope in Scala companion objects:
class MyClass { private def privateMethod(): Unit = { // Implementation } } object MyClass { def usePrivateMethod(): Unit = { val instance = new MyClass instance.privateMethod() } }
Encapsulation and method visibility in Scala:
class EncapsulationExample { private var internalData: Int = 42 def getData(): Int = { // Some logic internalData } def setData(newValue: Int): Unit = { // Some logic internalData = newValue } }
Scala method scope and pattern matching:
class PatternMatchingExample { def processData(data: Any): Unit = { data match { case _: String => println("String data") case _: Int => println("Integer data") case _ => println("Other data") } } }
Advanced method visibility techniques in Scala:
class AdvancedVisibilityExample { private[this] var internalData: Int = 42 def getData(implicit view: Int => String): String = { view(internalData) } }