×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Field overriding in Scala

By IncludeHelp Last updated : October 26, 2024

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.

Rules for Overriding Fields

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.

Example 1: 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

Example 2: 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...!

Example 3: 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.