Scala | Final - GeeksforGeeks

02 Sep, 2021
In Scala, Final is a keyword and used to impose restriction on super class or parent class through various ways. We can use final keyword along with variables, methods and classes.Following are the ways of using final keyword in Scala
Scala Final Variable: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 initialized by following ways:While declaringIn static blockIn ConstructorScalaScala// Scala program of using final variableclass Shapes{     // define final variable    final val area:Int = 60} class Rectangle extends Shapes{     override val area:Int = 100    def display()    {         println(area)     } }   // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         var b = new Rectangle()         b.display()     } }Following error occurs while running above codeOutput : 
prog.scala:5: error: overriding value area in class Shapes of type Int;
 value area cannot override final member
override val area:Int = 100
             ^
one error found
Scala Final Methods:Final method CalArea in the parent class (Shapes) indicate that, method cannot override in a child class (Rectangle).ScalaScala// Scala program of using final methodclass Shapes{     val height:Int = 0    val width :Int =0          // Define final method    final def CalArea(){     }} class Rectangle extends Shapes{     override def CalArea()    {         val area:Int = height * width         println(area)     } }   // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         var b = new Rectangle()         b.CalArea()     } }Following error occurs while running above codeOutput :prog.scala:8: error: overriding method CalArea in class Shapes of type ()Unit;
 method CalArea cannot override final member
    override def CalArea(){
                     ^
one error found
Scala Final ClassesIf the class in Scala is final then it cannot inherit to derived class. Inheritance restriction will be added by final keyword. Here if class Shapes are final then its all members also final and cannot used in derived class.ScalaScala// Scala program of using final classfinal class Shapes{     // Final variables and functions    val height:Int = 0    val width :Int =0    final def CalArea()    {     }} class Rectangle extends Shapes{     // Cannot inherit Shapes class      override def CalArea()     {         val area:Int = height * width         println(area)     } }   // Creating Objectobject GFG{     // Main method    def main(args:Array[String])    {         var b = new Rectangle()         b.CalArea()               } }Following error occurs while running above codeOutput : 
prog.scala:4: error: illegal inheritance from final class Shapes
class Rectangle extends Shapes{
                        ^
one error found

Scala Final Variable: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 initialized by following ways:While declaringIn static blockIn ConstructorScalaScala// Scala program of using final variableclass Shapes{     // define final variable    final val area:Int = 60} class Rectangle extends Shapes{     override val area:Int = 100    def display()    {         println(area)     } }   // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         var b = new Rectangle()         b.display()     } }Following error occurs while running above codeOutput : 
prog.scala:5: error: overriding value area in class Shapes of type Int;
 value area cannot override final member
override val area:Int = 100
             ^
one error found

Scala final variable initialized by following ways:
While declaring
In static block
In Constructor
Scala
// Scala program of using final variableclass Shapes{     // define final variable    final val area:Int = 60} class Rectangle extends Shapes{     override val area:Int = 100    def display()    {         println(area)     } }   // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         var b = new Rectangle()         b.display()     } }
Output :
 
prog.scala:5: error: overriding value area in class Shapes of type Int;
 value area cannot override final member
override val area:Int = 100
             ^
one error found

Scala Final Methods:Final method CalArea in the parent class (Shapes) indicate that, method cannot override in a child class (Rectangle).ScalaScala// Scala program of using final methodclass Shapes{     val height:Int = 0    val width :Int =0          // Define final method    final def CalArea(){     }} class Rectangle extends Shapes{     override def CalArea()    {         val area:Int = height * width         println(area)     } }   // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         var b = new Rectangle()         b.CalArea()     } }Following error occurs while running above codeOutput :prog.scala:8: error: overriding method CalArea in class Shapes of type ()Unit;
 method CalArea cannot override final member
    override def CalArea(){
                     ^
one error found

Scala
// Scala program of using final methodclass Shapes{     val height:Int = 0    val width :Int =0          // Define final method    final def CalArea(){     }} class Rectangle extends Shapes{     override def CalArea()    {         val area:Int = height * width         println(area)     } }   // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         var b = new Rectangle()         b.CalArea()     } }
Output :
prog.scala:8: error: overriding method CalArea in class Shapes of type ()Unit;
 method CalArea cannot override final member
    override def CalArea(){
                     ^
one error found

Scala Final ClassesIf the class in Scala is final then it cannot inherit to derived class. Inheritance restriction will be added by final keyword. Here if class Shapes are final then its all members also final and cannot used in derived class.ScalaScala// Scala program of using final classfinal class Shapes{     // Final variables and functions    val height:Int = 0    val width :Int =0    final def CalArea()    {     }} class Rectangle extends Shapes{     // Cannot inherit Shapes class      override def CalArea()     {         val area:Int = height * width         println(area)     } }   // Creating Objectobject GFG{     // Main method    def main(args:Array[String])    {         var b = new Rectangle()         b.CalArea()               } }Following error occurs while running above codeOutput : 
prog.scala:4: error: illegal inheritance from final class Shapes
class Rectangle extends Shapes{
                        ^
one error found

Scala
// Scala program of using final classfinal class Shapes{     // Final variables and functions    val height:Int = 0    val width :Int =0    final def CalArea()    {     }} class Rectangle extends Shapes{     // Cannot inherit Shapes class      override def CalArea()     {         val area:Int = height * width         println(area)     } }   // Creating Objectobject GFG{     // Main method    def main(args:Array[String])    {         var b = new Rectangle()         b.CalArea()               } }
Output :
 
prog.scala:4: error: illegal inheritance from final class Shapes
class Rectangle extends Shapes{
                        ^
one error found

9607whel
Picked
Scala
Scala-Inheritance
Scala-Keyword
Scala-OOPS
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Hello World in Scala
Scala Map get() method with example
Scala ListBuffer
Scala List map() method with example
How to install Scala on Windows?
Inheritance in Scala
Scala | Case Class and Case Object
Scala | Traits
Scala List length() method with example