Object-Oriented Programming in Scala

Scala treats everything as objects and supports key object-oriented programming (OOP) concepts such as classes, objects, constructors, inheritance, and traits.

Classes and Objects

  • Classes: A class in Scala is a blueprint for creating objects. It can contain fields and methods.

      class Person(var name: String, var age: Int) {
      def greet(): Unit = {
        println(s"Hello, my name is $name and I am $age years old.")
      }
    }
      
  • Objects: An instance of a class is known as an object. You create an object using the new keyword.

      val person1 = new Person("Kanye", 300)
    person1.greet()
      

Primary and Auxiliary Constructors

  • Primary Constructor: Defined in the class signature. It can have default values.

      class Person(var name: String = "Unknown", var age: Int = 0)
      
  • Auxiliary Constructor: Allows you to have additional constructors. They must call the primary constructor.

      class Person(var name: String, var age: Int) {
      def this(name: String) = this(name, 0) // Auxiliary Constructor
    }
      

Inheritance and Traits

  • Inheritance: Scala supports single inheritance, where a class can extend another class.

      class Employee(name: String, age: Int, var salary: Double) extends Person(name, age)
      
  • Traits: Similar to interfaces in Java, traits are used to define object types by specifying the signature of the supported methods.

      trait Greeting {
      def greet(message: String): Unit
    }
    class Person(var name: String) extends Greeting {
      def greet(message: String): Unit = println(s"$name says: $message")
    }
      

Code Example: Object-Oriented Scala Program

Here’s an example combining these OOP concepts:

  trait Greeting {
  def greet(): Unit
}

class Person(var name: String, var age: Int) extends Greeting {
  override def greet(): Unit = {
    println(s"Hello, I'm $name and I am $age years old.")
  }
}

object MainApp {
  def main(args: Array[String]): Unit = {
    val person1 = new Person("Alice", 25)
    person1.greet()
  }
}
  

This program defines a Person class with a trait Greeting. When we create an instance of Person, it can use the greet method from the trait.

Learn How To Build AI Projects

Now, if you are interested in upskilling in 2024 with AI development, check out this 6 AI advanced projects with Golang where you will learn about building with AI and getting the best knowledge there is currently. Here’s the link.

Last updated 17 Aug 2024, 12:31 +0200 . history