Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
Scala - Objects & Classes - Testingpool

Scala – Objects & Classes

In this tutorial, we will learn about objects and classes in scala which comes under OOPs concepts. In OOP, it is very important to understand what is an object and a class. Let’s understand them in details below.

What is an Object?

OK, so you will also find objects around you. In real world scenario, We can say everything is an object like car, fan, pen etc.An object has 2 characteristic i.e. State and behavior and it can be physical and logical. Let’s understand with examples.

A car has a state (name, color,model etc.) and behavior (driving,applying break, horn etc). Same way pen has a state (amount of ink,lid with on and off feature) and behavior (can be used to write etc.). These are the physical entities.

If we talk about non-physical things or logical objects, you can consider your bank account. A bank account can not be touched physically but has a state (the amount of money in it) and behaviour ( depositing or withdrawing money).

How does it relate to the programming model?

Object oriented programming is programming that involves intellectual objects that helps us to achieve or resolve business problems in real world.

In real world, you will find many objects of the same kind. There are many cars in the world, but each car is built from same set of blueprints and have same components.Class is a blueprint, from which individual objects are created.Consider a class name as CARS, Your car A is an instance of the class CARS , similar way your neighbour’s car B is also instance of the class CARS.

Class Objects

Let’s understand it with programming perspective. Once you define class named as Car, you can create object BMW from the class blueprint with the keyword new.Through that object, you can use all functionalities of the defined class.

package main.testingpool

class Car {
  var color : String = null //All fields must be Initialized 
  var speed: Int = 0

}

object BMW {

  def main(args:Array[String]): Unit ={
    var bmw = new Car   //Creating an object with new keyword
    bmw.color = "blue"
    bmw.speed =150

    println("BMW is of color " + bmw.color + " and having speed of "+ bmw.speed)
  }

  }
Output - BMW is of color blue and having speed of 150

In the above program, an object ‘bmw’ is created through class blueprint ‘Car’. There are few points to remember –

  • All Variables must be initialized in the class.
  • If no scope is specifies, it is public.
  • You must create an object which will contain the main method. This is the start point of the program.

Will the below program fail, as object does not have main method defined?

package main.testingpool

class Car {
  var color : String = null //All fields must be Initialized
  var speed: Int = 0
}

object BMW extends App{

    var bmw = new Car
    bmw.color = "blue"
    bmw.speed =150
    println("BMW is of color " + bmw.color + " and having speed of "+ bmw.speed)
}

Answer is NO. Because Object is extending Trait App which converts objects into executable program. We will see later what is Trait. As of now, you can just think Traits like interfaces in Java.

Another Example of class is given below, where constructor is created in the class definition. It is called primary constructor. We will learn about constructor in coming chapters.

package main.testingpool

class Car(color : String,speed: Int) {   //Primary Constructor
  def carFeature: Unit ={
    println("BMW is of color " + color + " and having speed of "+ speed)
  }
}

object BMW extends App{
    var bmw = new Car("blue",150)
    bmw.carFeature
}

Scala Anonymous object:

In Scala, we can create anonymous object. An object which does not have any reference name, is called the anonymous object. These objects can be created if we don’t want to use them further. As we have modified the above code and called the method carFeature directly with anonymous object new car.

package main.testingpool

class Car(color : String,speed: Int) {   //Primary Constructor
  def carFeature: Unit ={
    println("BMW is of color " + color + " and having speed of "+ speed)
  }
}

object BMW extends App{
  new Car("blue",150).carFeature
}
Output - BMW is of color blue and having speed of 150
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...