Classes and Object

    A class can be defined as an entity or template with some state(variables) and behaviour (methods). Any real-world object can be considered as a Class which possesses some attributes and can perform some functionalities.

    Giving meaning to class creates objects where the blueprint gets actual values. In other words, it is an encapsulation of data along with functions that act upon that data.

    Example:

    • Phone is a class.

    Attributes: Brand , RAM, Camera, Price, ModelName

    Functionality: canMakeCall(), canClickPictures() etc.

    • One Plus 7 Pro is an Object with some specific details provided to above attribute.
    • IPhone13 Pro is another example of an object.

    Comparison between class and object

    A class is a logical entity.

    An object is a physical entity.

    When a class is created, no memory space is allocated.

    On object creation, memory space is allocated.

    A class is created using a class keyword.

    An object is created using a new keyword

    class Light(power: Int, color: String, state: String)

    val bulb = new Light(100, "White", "ON")

    Output in Scala worksheet
    class Light

    Output in Scala worksheet
    val bulb: Light = Light@7c339732

    Parameters of a class, (power, color and state in above example) are val by default and cannot be accessed from outside the class. However, you can explicitly define them to be var.

    Example:

    bulb.power     // This will lead to error???????

    Adding one method to class:

    class Light(power: Int, color: String, state: String) {
      override def toString: String = {
        s"I light up to ${power} watt in ${color} color" +
          s" My current state is ${state} state"
      }
    }
    
    val bulb = new Light(100, "White", "ON")

    Output:

    class Light
    
    val bulb: Light = I light up to 100 watt in White color My current state is ON state

    Auxilliary Constructor

    Constructors are used to initialise the state of the objects. In the above examples, one Primary Constructor is by default created. For any other type of object creation(different argument types or the different number of argument types). We need to define an auxiliary constructor.

    Syntax:

    Auxiliary Constructors are defined using “this” keyword, similar to methods. A scala class can have as many auxiliary constructors but only one Primary Constructor.

    Example :

    class Light(power: Int) {    // Primary Constructor
    
      var color: String = "WHITE"
      var state: String = "OFF"
    
      // Auxilliary constructor
      def this(pow: Int, color: String) {
        this(pow) // Invoking primary constructor
        this.state = "OFF"
      }
    
      override def toString: String = {
        s"I light up to ${power} watt in ${color} color" +
          s" My current state is ${state} state"
      }
    }
    
    
    val bulb = new Light(100, "YELLOW")

    Output:

    class Light
    
    val bulb: Light = I light up to 100 watt in WHITE color My current state is OFF state

    Some rules to consider while defining Auxilliary Constructors:

    • We can define the possible number of auxiliary constructors having either different signature or different parameter list.
    • Each auxiliary constructor must call either the primary constructor or previously defined auxiliary constructor that comes previous to the current constructor(in top to bottom flow), which we are defining
    • First statement inside the auxiliary constructor should use this keyword.

    Default Parameters

    Default parameters are the ones in which you can provide a static value to any field, which will be used if a value is not provided explicitly. If a value is provided explicitly, then the provided value will be used.

    Example:

    In the below example, balance is the default parameter.

    class Account(accountNumber: Int, accountName: String, balance:Long = 0) {
    
     def debit(amount: Long) = {
       balance - amount
     }
    
     def credit(amount: Long) = {
       balance + amount
     }
    }
    
    val account1 = new Account(1, "Vijay", 100000)
    account1.debit(100)
    
     
    val account2 = new Account(1, "Vijay")
    account2.credit(100)

    Output:

    val account1: Account = Account@14f1874d
    val res6: Long = 99900
    
    
    val account2: Account = Account@47e7e7ad
    val res7: Long = 100