Home » Scala language

Field overriding in Scala

Scala field overriding: In Scala, field overriding is also allowed to redefine the field in the child class. In this tutorial, we will learn about the field overriding with the use of examples.
Submitted by Shivang Yadav, on September 17, 2019

Scala field overriding

Overriding is the concept in which the child class is allowed to redefine the members of the parent class. Both methods and variables/ fields can be overridden in object-oriented programming. In Scala as well both methods and Fields can be overridden but overriding of fields has some restrictions on implementation.

Overriding fields in Scala

For overriding fields in Scala, some rules must be kept in mind to avoid errors that arise while implementing this concept,

  1. The use of override keyword is necessary to implement the concept field overriding. If you missed the keyword an error will be prompted and execution of the code will be stopped.
  2. Only fields that are defined using the val keyboard for defining fields in both parent class and child class can be overridden.
  3. Fields that are defined using the var keyword cannot be overridden as they are editable ( that is both read and write operations are allowed on the variable) anywhere in the class.

Here are some programs that show the implementation of field overriding and Scala.

1) Python program without override keyword

This program will show what will be the output if override keyword is not used?

class animal{  
    val voice = "animal's voice" 
}  

class dog extends animal{  
   val voice = "Dog bark's...!"  
    def says(){  
        println(voice)  
    }  
}  

class cat extends animal{
    val voice = "cat meow's...!";
    def says(){  
        println(voice)  
    } 
}

object MainObject{  
    def main(args:Array[String]){  
        var ani = new dog()  
        ani.says()  
    }  
} 

Output

error: overriding value voice in class animal of type String;
 value voice needs `override' modifier
   val voice = "Dog bark's...!"
       ^
error: overriding value voice in class animal of type String;
 value voice needs `override' modifier
    val voice = "cat meow's...!";
        ^
two errors found

2) Python program with override keyword

This program will show what will be the output if override keyword is used?

class animal{  
    val voice = "animal's voice"
}  

class dog extends animal{  
   override val voice = "Dog bark's...!"  
    def says(){  
        println(voice)  
    }  
}  

class cat extends animal{
    override val voice = "Cat meow's...!";
    def says(){  
        println(voice)  
    } 
}

object MainObject{  
    def main(args:Array[String]){  
        var ani = new dog()  
        ani.says()  
        var ani2 = new cat()
        ani2.says()
    }  
} 

Output

Dog bark's...!
Cat meow's...!

3) Python program with var keyword

This program will show what will be the output if var keyword is used?

class animal{  
    var voice = "animal's voice"
}  

class dog extends animal{  
   override var voice = "Dog bark's...!"  
    def says(){  
        println(voice)  
    }  
}

class cat extends animal{
    override var voice = "Cat meow's...!";
    def says(){  
        println(voice)  
    } 
}

object MainObject{  
    def main(args:Array[String]){  
        var ani = new dog()  
        ani.says()  
        var ani2 = new cat()
        ani2.says()
    }  
}  

Output

error: overriding variable voice in class animal of type String;
 variable voice cannot override a mutable variable
   override var voice = "Dog bark's...!"
                ^
error: overriding variable voice in class animal of type String;
 variable voice cannot override a mutable variable
    override var voice = "Cat meow's...!";
                 ^
two errors found


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.