You are currently viewing Scala Object-Oriented Programming (OOPs) Concepts Explained with Examples

Scala Object-Oriented Programming (OOPs) Concepts Explained with Examples

1. Classes and Objects:
Scala, like many other object-oriented languages, revolves around classes and objects. Classes serve as blueprints for creating objects, while objects are instances of classes.

class Person(var name: String, var age: Int) {
  def displayDetails(): Unit = {
    println(s"Name: $name, Age: $age")
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val person1 = new Person("Alice", 30)
    person1.displayDetails() // Output: Name: Alice, Age: 30
  }
}

2. Inheritance:
Scala supports single and multiple inheritance. Classes can inherit fields and methods from a single parent class.

class Animal(var species: String) {
  def sound(): Unit = {
    println("Some generic sound")
  }
}

class Dog(species: String, var breed: String) extends Animal(species) {
  override def sound(): Unit = {
    println("Woof!")
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val dog = new Dog("Canine", "Labrador")
    dog.sound() // Output: Woof!
  }
}

3. Polymorphism:
Polymorphism in Scala allows methods to behave differently based on the object calling them.

class Shape {
  def area(): Double = {
    0.0
  }
}

class Circle(radius: Double) extends Shape {
  override def area(): Double = {
    math.Pi * radius * radius
  }
}

class Rectangle(width: Double, height: Double) extends Shape {
  override def area(): Double = {
    width * height
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val shapes = List(new Circle(5), new Rectangle(3, 4))
    shapes.foreach(shape => println(s"Area: ${shape.area()}")) // Output: Area: 78.53981633974483, Area: 12.0
  }
}

4. Encapsulation:
Scala supports encapsulation by allowing you to control access to members of a class.

class BankAccount(private var balance: Double) {
  def deposit(amount: Double): Unit = {
    balance += amount
  }

  def withdraw(amount: Double): Unit = {
    if (balance >= amount) {
      balance -= amount
    } else {
      println("Insufficient balance")
    }
  }

  def getBalance(): Double = {
    balance
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val account = new BankAccount(1000)
    account.deposit(500)
    account.withdraw(200)
    println(s"Balance: ${account.getBalance()}") // Output: Balance: 1300.0
  }
}

5. Abstraction:
Abstraction in Scala involves hiding the implementation details and showing only the necessary features of an object.

abstract class Shape {
  def area(): Double
}

class Circle(radius: Double) extends Shape {
  override def area(): Double = {
    math.Pi * radius * radius
  }
}

class Rectangle(width: Double, height: Double) extends Shape {
  override def area(): Double = {
    width * height
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val shapes = List(new Circle(5), new Rectangle(3, 4))
    shapes.foreach(shape => println(s"Area: ${shape.area()}")) // Output: Area: 78.53981633974483, Area: 12.0
  }
}

Conclusion:
Scala provides robust support for Object-Oriented Programming (OOPs) concepts, enabling developers to write clean, modular, and maintainable code. By mastering these concepts, you’ll be well-equipped to build scalable and efficient applications in Scala.

Leave a Reply