Differentiation of Scala Field Overriding with Examples

If You are interested to learn about the Scala Hash Maps

You can override fields in Scala as well, however there are specific guidelines that must be observed. The examples that follow show you how to override fields. Overriding is a feature that lets a subclass in any object-oriented programming language give a particular implementation of a method or field that is already provided by one of its super-classes. In contrast to Overriding in Scala, which allows both methods and fields to be overridden with a few mandatory requirements, Overriding in Scala is more explicitly specified.

Rules for the Field Overriding

The rules for the field overriding are as follows:  

  • One of the most important rules is that when overriding fields from the super-class in the sub-classes, we must use the keyword override or override notation; otherwise, the compiler will raise an error and stop the programme from running.
  • We must override variables that are specified using simply the val keyword in both the super class and the sub-classes in order to carry out a Field Overriding.
  • Because we may read and write to var, field overriding cannot override var.

Scala Field Overriding Example1

class Vehicle{  
    var speed:Int = 60  
  
}  
class Bike extends Vehicle{  
   var speed:Int = 100  
    def show(){  
        println(speed)  
    }  
}  
object MainObject{  
    def main(args:Array[String]){  
        var b = new Bike()  
        b.show()  
    }  
} 

Output:

Error - variable speed needs 'override' modifier.

In scala, you must use either override keyword or override annotation when you are overriding methods or fields of super class. If you don’t do this, compiler reports an error and stops execution of program.


Scala Field Overriding Example2

class Vehicle{  
     val speed:Int = 60  
  
}  
class Bike extends Vehicle{  
   override val speed:Int = 100     // Override keyword  
    def show(){  
        println(speed)  
    }  
}  
object MainObject{  
    def main(args:Array[String]){  
        var b = new Bike()  
        b.show()  
    }  
} 

Output:

100

In scala, you can override only those variables which are declared by using val keyword in both classes. Below are some interesting examples which demonstrate the whole process.

Scala Field Overriding Example3

class Vehicle{  
     var speed:Int = 60  
}  
class Bike extends Vehicle{  
   override var speed:Int = 100  
    def show(){  
        println(speed)  
    }  
}  
object MainObject{  
    def main(args:Array[String]){  
        var b = new Bike()  
        b.show()  
    }  
} 

Output:

variable speed cannot override a mutable variable

Scala Field Overriding Example4

class Vehicle{  
     val speed:Int = 60  
  
}  
  
class Bike extends Vehicle{  
   override var speed:Int = 100  
    def show(){  
        println(speed)  
    }  
}  
  
object MainObject{  
    def main(args:Array[String]){  
        var b = new Bike()  
        b.show()  
    }  
}  

Output:

Error - variable speed needs to be a stable, immutable value
Differentiation of Scala Field Overriding with Examples
Show Buttons
Hide Buttons