×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Access modifiers in Scala

By IncludeHelp Last updated : October 22, 2024

Access Modifiers

Access modifiers are used in order to restrict the usage of a member function to a class or a package. Using access modifiers data hiding takes place which is a very important concept of OOPs.

The access to a class, object or a package can be restricted by the use of three types of access modifiers that are:

  • public: Accessible to everyone
  • private: Accessible only in the class
  • protected: Accessible to class and its subclasses

Public access modifier

It is the default type of modifier in Scala. In Scala, if you do not use any access modifier then the member is public. Public members can be accessed from anywhere.

Syntax

def function_name(){}
or 
public def fuction_name(){}

Example

class Person {
  // Public members (default in Scala)
  var name: String = "Natalia"
  var age: Int = 25

  // Public method
  def displayInfo(): Unit = {
    println(s"Name: $name")
    println(s"Age: $age")
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val person = new Person

    // Accessing public fields from outside the class
    println(person.name)  // prints: Natalia
    println(person.age)   // prints: 25

    // Calling public method
    person.displayInfo()  // prints: Name: Natalia, Age: 25

    // Modifying public fields
    person.name = "Natalia"
    person.age = 30

    // Accessing the updated fields
    println(person.name)  // prints: Natalia
    println(person.age)   // prints: 30
  }
}

The output will be:

Natalia
25
Name: Natalia
Age: 25
Natalia
30

Private access modifier

In private access, access to the private member is provided only to other members of the class (block). It any call outside the class is treated as an error.

Syntax

private def function_name(){}

Example

class Person(private var name: String, private var age: Int) {
  // Public method to access private member
  def displayInfo(): Unit = {
    println(s"Name: $name, Age: $age")
  }

  // Private method, only accessible within this class
  private def isAdult(): Boolean = age >= 18

  // Public method can call the private method
  def checkAdultStatus(): Unit = {
    if (isAdult()) {
      println(s"$name is an adult.")
    } else {
      println(s"$name is not an adult.")
    }
  }
}

object Demo {
  def main(args: Array[String]): Unit = {
    val person = new Person("Natalia", 25)

    // Accessing public method
    person.displayInfo()

    // Accessing public method that calls a private method
    person.checkAdultStatus()

    // Trying to access private members directly (this will cause an error)
    // println(person.name)    // Error: name is private
    // println(person.isAdult()) // Error: isAdult() is private
  }
}

The output will be:

Name: Natalia, Age: 25
Natalia is an adult.

Protected access modifier

In protected access, the availability of the member function is limited to the same class and its subclass. Excess without inheritance is treated as an error.

Syntax

protected def function_name(){}

Example

class Animal {
  protected var name: String = "Unknown"

  def printName(): Unit = {
    println(s"Animal name: $name")
  }
}

class Dog extends Animal {
  def setName(newName: String): Unit = {
    name = newName  // Accessing protected member from subclass
  }

  def printDogInfo(): Unit = {
    println(s"Dog's name: $name")
  }
}

object Demo {
  def main(args: Array[String]): Unit = {
    val dog = new Dog
    dog.setName("Buddy")
    dog.printDogInfo()  // Accessing protected member indirectly through subclass
    dog.printName()      // Accessing method in superclass

    // dog.name = "Max"  // Error: Cannot access protected member directly
  }
}

The output will be:

Dog's name: Buddy
Animal name: Buddy

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.