Scala final Keyword

In this tutorial, we will learn about the final keyword with example in Scala. By Shivang Yadav Last updated : April 02, 2023

The 'final' Keyword

In Scala, a variable that is declared using the final keyword cannot be further changed in the program. This means it is declared only once in the parent class and will remain unchanged (constant) throughout the program.

Syntax

final val variable_name : datetype = value;

In many cases, we come around situations when we need some instances to be constant so show the final keyword is used to make them immune to changes in child class.

Scala Example of Final Keyword

Let's take an example of cars, by default every car has four wheels so we can make the number of Wheels of the final variable so that it cannot be changed in any class that inherits the base class car.

class car{
    final val wheels : Int = 4; 
}

class honda extends car{
    val wheels : Int = 3;
    override def wheel(){
        println(wheels)
    }
}

object MyClass {
    def main(args: Array[String]) {
        var newcar = new honda(); 
        newcar.wheel()
    }
}

Output

error: overriding value wheels in class car of type Int;
 value wheels cannot override final member
    val wheels : Int = 3;
        ^
error: method wheel overrides nothing
    override def wheel(){
                 ^
two errors found

Scala Final Method

In Scala, a Method can also be declared as final, so that no child class will be able to override the method. this makes the code in the method Universal code that can be used by others but cannot be manipulated.

Syntax

final def method_name() { }

Scala Example of Final Method

Let's take an example of cars, some functioning like steering of each car is the same. so the steering methods of the class car can be defined as final so that no other class will be able to override it.

class car{
    final val wheels : Int = 4; 
    final def drive(){
        println("!! Broom !")
    }
}

class honda extends car{
     override def drive(){
        println("!! Broom Broom !")
    }
}

object MyClass {
    def main(args: Array[String]) {
        var newcar = new honda(); 
        newcar.drive()
    }
}

Output

error: overriding method drive in class car of type ()Unit;
 method drive cannot override final member
     override def drive(){
                  ^
one error found

Scala Final Class

Using the final keyword with a class, the class can be made immune to inheritance i.e. inheriting a class defined with Final keyword this is not possible.

Syntax

final class class_name {
}

Scala Example of Final Class

If the car class is made final none of the objects of the class can be accessed from another class.

final class car{
    val wheels : Int = 4; 
    def drive(){
        println("!! Broom !")
    }
}

class honda extends car{
    override val wheels = 34;   
}

object MyClass {
    def main(args: Array[String]) {
        var newcar = new honda(); 
        newcar.drive()
    }
}

Output

error: illegal inheritance from final class car
class honda extends car{
                    ^
one error found





Comments and Discussions!

Load comments ↻






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