Home » Scala language

final keyword with example in Scala

Scala final keyword: The final keyword in Scala is used to restrict the usage of a class. This will learn about the final keyword in Scala, its usage and working examples.
Submitted by Shivang Yadav, on September 17, 2019

Scala 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.

Example:

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.

Code:

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

final method in Scala

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() { }

Example:

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.

Code:

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

final class in Scala

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 {
    }

Example:

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

Code:

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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.