Scala | Field Overriding

In any object-oriented programming language, Overriding is a feature that allows a subclass to provide a specific implementation of a method or field that is already provided by one of its super-classes. In Scala, Overriding is further explicitly stated in comparison to Overriding in Java, as here both methods as well as Fields can be overridden but it has a few restrictions which are obligatory to follow.

Rules for the Field Overriding

The rules for the field overriding are as follows:

  • The one of the foremost rule is that we must utilize the keyword override or override notation while overriding the fields of the super-class in the sub-classes otherwise the compiler will throw an error and will terminate the execution of the program.
  • In order to execute a Field Overriding, we need to override variables that are declared utilizing only the val keyword in both super class as well as sub-classes.
  • Field overriding cannot override var, because we can both read as well as write var.

Now, lets see some examples to illustrate these restrictions.



Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of Field 
// Overriding
  
// Creating class
class Shapes
{
  
    // Creating a variable with 
    // val keyword
    val description:String = "shape"
  
// Creating a subclass
class shape_1 extends Shapes
  
    // Overriding field using
    // 'override' keyword 
    override val description:String ="It is a circle."
  
    // Defining a method
    def display()
    
      
        // Displays output
        println(description) 
    
  
// Creating a subclass
class shape_2 extends Shapes
  
    // overriding field using
    // 'override' keyword 
    override val description:String ="It is a square."
  
    // Defining a method
    def display()
    
      
        // Displays output
        println(description) 
    
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    
      
        // Creating instances for all
        // the sub-classes
        var x = new shape_1() 
        var y = new shape_2() 
      
        // Calling methods
        x.display()
        y.display()
      
    

chevron_right


Output:

It is a circle.
It is a square.

Now, we can see in above example that val is utilized in both super class and subclass so, overriding of the field was practicable otherwise it would have thrown an error. Here, the field in the super class is val which is overridden in the sub-classes using the override keyword.

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of Field 
// Overriding
  
// Creating class
class Shapes
{
  
    // Creating a variable with 
    // val keyword
    val description:String = "shape"
  
// Creating a subclass
class shape_1 extends Shapes
  
    // Overriding field using
    // 'override' keyword 
    override var description:String ="It is a circle."
      
    // Defining a method
    def display()
    
      
        // Displays output
        println(description) 
    
  
// Creating a subclass
class shape_2 extends Shapes
  
    // overriding field using
    // 'override' keyword 
    override var description:String ="It is a square."
  
    // Defining a method
    def display()
    
      
        // Displays output
        println(description) 
    
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    
      
        // Creating instances for all
        // the sub-classes
        var x = new shape_1() 
        var y = new shape_2() 
      
        // Calling methods
        x.display()
        y.display()
      
    

chevron_right


Output:

prog.scala:17: error: overriding value description in class Shapes of type String;
variable description needs to be a stable, immutable value
override var description:String =”It is a circle.”
^
prog.scala:32: error: overriding value description in class Shapes of type String;
variable description needs to be a stable, immutable value
override var description:String =”It is a square.”
^
two errors found

It is the same example as above but here errors are found as here, var is used in the sub-classes to override the fields which is not possible as val cannot be overridden by the var as stated above.

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of Field 
// Overriding
  
// Creating class
class Animals
{
  
    // Creating a variable with 
    // 'var' keyword
    var number:Int = 2
  
// Creating a subclass
class Cat extends Animals
  
    // Overriding field using
    // 'override' keyword 
    override var number:Int = 4
      
    // Defining a method
    def show()
    
      
        // Displays output
        println("We have " + number + " cats."
    
  
// Creatiing object
object GfG
  
    // Main method
    def main(args:Array[String])
    
      
        // Creating instance of the
        // sub-class
        var cat = new Cat() 
      
        // Calling method
        cat.show()
      
    

chevron_right


Output:

prog.scala:17: error: overriding variable number in class Animals of type Int;
variable number cannot override a mutable variable
override var number:Int = 4
^
one error found

Here, var is utilized for overriding fields which is not possible as stated in the above rules so, an error is found.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.