Classes and objects in scala

Category : Scala | Sub Category : Scala Programs | By Prasad Bonam Last updated: 2023-10-21 03:24:25 Viewed : 261


In Scala, classes and objects are fundamental building blocks used for defining and creating reusable components. Here is an overview of classes and objects in Scala:

  1. Classes:

    • Classes are blueprints that define the properties and behaviors of objects.
    • They can contain fields, methods, constructors, and nested types.
    • Instances of classes are created using the new keyword.
    • Example of a simple class:
    scala
    class MyClass(var name: String, var age: Int) { def display(): Unit = { println(s"Name: $name, Age: $age") } }


  2. Objects:

    • Objects are single instances of their own definitions, often used for utility methods or to contain a single instance of a class.
    • They are defined using the object keyword.
    • Objects can extend classes or traits, and they can be used as entry points to a Scala application.
    • Example of a simple object:
    scala
    object MyObject { def myMethod(): Unit = { println("This is a method in MyObject") } }
  3. Companion Objects:

    • Companion objects have the same name as a class and are defined in the same file as the class.
    • They can access the private members of the corresponding class, and the class can access the private members of the companion object.
    • Companion objects are commonly used for factory methods and other shared behavior.
    • Example of a class and its companion object:
    scala
    class MyClass(var name: String, var age: Int) { def display(): Unit = { println(s"Name: $name, Age: $age") } } object MyClass { def apply(name: String, age: Int): MyClass = { new MyClass(name, age) } }

Understanding classes and objects is essential for creating and organizing complex applications in Scala. Classes define the structure and behavior of objects, while objects provide a way to group related methods and act as entry points or utility components in the application. Companions objects provide a convenient way to associate common functionalities with a class.


Example of a Class:

scala
class Person(var name: String, var age: Int) { def display(): Unit = { println(s"Name: $name, Age: $age") } } // Creating an instance of the Person class val person = new Person("John", 30) person.display()

Output:

yaml
Name: John, Age: 30

Example of an Object:

scala
object MathUtils { def square(x: Int): Int = x * x } // Accessing the method from the MathUtils object val result = MathUtils.square(5) println(result)

Output:

25

Example of a Class and its Companion Object:

scala
class Person(var name: String, var age: Int) { def display(): Unit = { println(s"Name: $name, Age: $age") } } object Person { def apply(name: String, age: Int): Person = { new Person(name, age) } } // Creating an instance of the Person class using the companion object val person = Person("Alice", 25) person.display()

Output:

yaml
Name: Alice, Age: 25

In these examples, the first one demonstrates the usage of a simple class Person, the second one showcases an object MathUtils with a utility method, and the third one illustrates the usage of a companion object for the Person class, providing a factory-like method apply to create instances of the Person class. Each example provides a clear understanding of how classes and objects work in Scala, along with their respective outputs.

Search
Related Articles

Leave a Comment: