Introduction to Scala Final Variable with Example

If You are interested to learn about the Scala Inheritance

Final is a keyword, which is used to prevent inheritance of super class members into derived class. You can declare final variables, methods and classes also. Scale final variable initialized only once while declared and used as constant throughout the program. In below example, variable area is final which is declared as final and also initialized while declare in superclass shapes. if we want to access or modify the variable area from derived class Rectangle then it is not possible because the restriction on variable area is added by keyword final.

Scala Final Variable Example

You can’t override final variables in subclass. Let’s see an example.

class;
Vehicle{
final val speed:Int = 60  

}  

class Bike extends Vehicle{  

   override val speed:Int = 100  

    def show(){  

        println(speed)  

    }  

}    

object MainObject{  

    def main(args:Array[String]){  

        var b = new Bike()  

        b.show()  
    }  
}

    Output:

Error - value speed cannot override final member 

Example – scala This- 

Let’s see how ‘ Scala This’ works. In the following example, we use ‘Scala This’ to call the primary constructor and the instance variables.

scala> class Marks{
| var midterm=0
| var finals=0
| def this(midterm:Int,finals:Int){
| this()
| this.midterm=midterm
| this.finals=finals | }
| def show(){
| println("You've got "+midterm+" in midterms and "+finals+" in finals”)
| }
| } 
defined class Marks
scala> var chemistry=new Marks(96,99) 
chemistry: Marks = Marks@7e3ee128
scala> chemistry.show()

Example- Scala This- 

scala> class Calculator(a:Int){
| def this(a:Int,b:Int){
| this(a)
| println(a+"+"+b+"="+{+b})
| }
| } 
defined class Calculator
scala> var c=new Calculator(3,4)
3+4=7
c: Calculator = Calculator@3723de42

Here, we call one constructor from another. This call statement has to be the first statement in this body.

Example- Scala This- 

scala> class Calculator(){
| def this(a:Int){
| this()
| println(a)
| }
| } 
defined class Calculator
scala> var c=new Calculator(3)
c: Calculator = Calculator@4e8036bb
scala> var d=new Calculator()
d: Calculator = Calculator@45c3cf0c

Scala Final Method

Final method declare in the parent class can’t be override. You can make any method to final if you don’t want to get it overridden. Attempt to override final method will cause to a compile time error.Play Video

Scala Final Method Example

class;
Vehicle{
}
final def show(){  

         println("vehicle is running")  
     }  
}  
class Bike extends Vehicle{  

   //override val speed:Int = 100  

    override def show(){  

        println("bike is running")  

    }  
}  

object MainObject{  

    def main(args:Array[String]){  

        var b = new Bike()  

        b.show()  

    }  

}

    Output:

method show cannot override final member
    override def show(){
                 ^
one error found

Scala Final Class Example

You can also make final class. Final class can’t be inherited. If you make a class final, it can’t be extended further.

finalclass Vehicle{ 
def show(){  
         println("vehicle is running")  
     }  
}  
class Bike extends Vehicle{  

       override def show(){  

        println("bike is running")  
    }  
}  

object MainObject{  

    def main(args:Array[String]){  

        var b = new Bike()  

        b.show()  
    }  
}

Output:

error: illegal inheritance from final class Vehicle
class Bike extends Vehicle{
                   ^
one error found
Introduction to Scala Final Variable with Example
Show Buttons
Hide Buttons