Object-Oriented Programming in Scala

Reading Time: 3 minutes

Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. Scala is also an Object-Oriented language and in this blog, we will see how we achieve that in scala.

Demystifying OOP in Scala. Scala is a must know language if you… | by  Faysal Ishtiaq | Medium

Classes in Scala

Class is like a blueprint for a real-world entity which we call object in programming languages. It provides the initial values for state (member variables or attributes), and implementations of behaviour (member functions or methods). In Scala, we do this by defining a class:

class Complex(real: Int, imag: Int) {
  def getReal = real
  def getImag = imag
}

This defines an entity called Complex that will have two state variables real and imag for defining the real and imaginary part of the complex number when we create an object using this class.

Objects

An object is an entity that we create using a blueprint (class) and has a state and behaviour. We define an object by two types in scala.

By new keyword

We can create an object of a class by using the new keyword in scala. For example, if we want to create an object of the Complex type then we can do it like-

val complexNum = new Complex(1, 2) 

This will create an object of Complex type with state variables real as 1 and imag as 2. We can change the state variables only if we declare them as mutable. We can do it using the var keyword

By Creating Singleton Object

A singleton object is an object that is created without explicitly defining a class. It is an object which defines a single object of a class. We can create a singleton object as follows-

object SingletonObject{  
    def hello(){  
        println("Hello, This is Singleton Object")  
    }  
}  

Inheritance In Scala

Inheritance in Object-Oriented Programming is a process in which one class inherits the attributes and methods of another class. We achieve inheritance in Scala using extends keyword. Here is an example-

  class Animal {
    val age: Int = 0
    def eat() = println("I'm eating")
  }
  class Dog(val name: String) extends Animal

Here is a class named Dog that extends the class Animal and now has features of an Animal. We can now access the eat method from an instance of Dog type like this-

val aDog = new Dog("Lassie")
aDog.eat()

This would print “I’m eating” on the console.

Subtype Polymorphism In Scala

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when we use a parent class reference to refer to a child class object. Here is an example of Subtype Polymorphism in Scala.

  class Animal {
    val age: Int = 0
    def eat() = println("I'm eating")
  }
  class Dog(val name: String) extends Animal

  val aDeclaredAnimal: Animal = new Dog("Hachi")
  aDeclaredAnimal.eat()

This would call the most derived method will at runtime. The type of aDeclaredAnimal is decided at runtime.

This would print “I’m eating” on the console.

Traits in Scala

A Trait is a concept in object-oriented programming, which represents a set of methods that can be used to extend the functionality of a class. It is the ultimate abstract type with all unimplemented methods and we implement them at the time of extending the trait.

Here is how we declare a trait in Scala-

  trait Carnivore {
    def eat(animal: Animal): Unit
  }

Now we can Implement a trait like this-

class Crocodile extends Carnivore {
    override def eat(animal: Animal): Unit = println("I am eating you, animal!")
}

Now we can use the eat method like this-

val aNewAnimal = new Animal
val aNewCrocodile = new Crocodile
aNewCrocodile.eat(aNewAnimal)

This would print “I am eating you, animal!” on the console.

This was a short introduction to Object-Oriented Programming in Scala. Stay tuned for more blogs like this.


Knoldus-blog-footer-image
knoldus