Horje
How to Access a Case Class Method from Another Class in Scala?

Accessing a case class method from another class in Scala can be achieved through several approaches. Case classes are commonly used to model immutable data and provide convenient methods for accessing and manipulating data fields. The article focuses on discussing different methods to access a case class method from another class in Scala.

Using Companion Object

Below is the Scala program to access a case class method from another class using a companion object:

Scala
case class Person(name: String) {
  def greet(): Unit = println(s"Hello, $name")
};

object AnotherClass {
  def main(args: Array[String]): Unit = {
    val person = Person("Alice");
    person.greet()
  }
};

Output
Hello, Alice

Explanation:

  1. We define a case class Person with a method greet() that prints a greeting message.
  2. In the AnotherClass object, we create an instance of Person and invoke the greet() method.

Using Inheritance

Below is the Scala program to access a case class method from another class using inheritance:

Scala
case class Person(name: String) {
  def greet(): Unit = println(s"Hello, $name")
};

class AnotherClass extends Person("Bob") {
  def invokeGreet(): Unit = greet()
};

object Main {
  def main(args: Array[String]): Unit = {
    val anotherClass = new AnotherClass;
    anotherClass.invokeGreet()
  }
};

Output
Hello, Bob

Explanation:

  1. We define a case class Person with a method greet().
  2. The AnotherClass class extends Person and defines a method invokeGreet() that calls the greet() method.
  3. In the Main object, we create an instance of AnotherClass and invoke the invokeGreet() method.

Using Implicit Conversion

Below is the Scala program to access a case class method from another class using implicit conversion:

Scala
object Main {
  def main(args: Array[String]): Unit = {
    import AnotherClass._;
    val person = Person("Charlie");
    person.invokeGreet();
  }
};
case class Person(name: String) {
  def greet(): Unit = println(s"Hello, $name");
};

object AnotherClass {
  implicit class PersonOps(person: Person) {
    def invokeGreet(): Unit = person.greet();
  }
};

Output
Hello, Charlie

Explanation:

  1. We define a case class Person with a method greet().
  2. In the AnotherClass object, we define an implicit class PersonOps that adds a method invokeGreet() to Person.
  3. In the Main object, we import the implicit conversion and use it to invoke the invokeGreet() method on a Person instance.

Conclusion

Accessing a case class method from another class in Scala can be accomplished using various techniques, such as companion objects, inheritance, or implicit conversions.




Reffered: https://www.geeksforgeeks.org


Scala

Related
How to Check for Null in a Single Statement in Scala? How to Check for Null in a Single Statement in Scala?
How to Use Sortby in Scala? How to Use Sortby in Scala?
How to Check the Schema of DataFrame in Scala? How to Check the Schema of DataFrame in Scala?
How to Escape Special Symbols in Scala String at Runtime? How to Escape Special Symbols in Scala String at Runtime?
How to Qualify Methods as Static in Scala? How to Qualify Methods as Static in Scala?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14