Scala | Final
In Scala, Final is a keyword and used to impose restriction on super class or parent class though 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 declaring
- In static block
- In Constructor
// Scala program of using final variableclassShapes{// define final variablefinalvalarea:Int=60}classRectangleextendsShapes{overridevalarea:Int=100defdisplay(){println(area)}}// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){varb=newRectangle()b.display()}}chevron_rightfilter_noneFollowing error occurs while running above code
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).// Scala program of using final methodclassShapes{valheight:Int=0valwidth:Int=0// Define final methodfinaldefCalArea(){}}classRectangleextendsShapes{overridedefCalArea(){valarea:Int=height * widthprintln(area)}}// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){varb=newRectangle()b.CalArea()}}chevron_rightfilter_noneFollowing error occurs while running above code
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 Classes
If 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.// Scala program of using final classfinalclassShapes{// Final variables and functionsvalheight:Int=0valwidth:Int=0finaldefCalArea(){}}classRectangleextendsShapes{// Cannot inherit Shapes classoverridedefCalArea(){valarea:Int=height * widthprintln(area)}}// Creating ObjectobjectGFG{// Main methoddefmain(args:Array[String]){varb=newRectangle()b.CalArea()}}chevron_rightfilter_noneFollowing error occurs while running above code
Output :
prog.scala:4: error: illegal inheritance from final class Shapes class Rectangle extends Shapes{ ^ one error found
Recommended Posts:
- Scala | Either
- Scala Map
- Set in Scala | Set-2
- Set in Scala | Set-1
- StringBuilder in Scala
- Scala Map get() method with example
- Remove key value from Map in Scala
- Scala | REPL
- Scala | Arrays
- Scala | ArrayBuffer
- while and do while Loop in Scala
- Scala Map last() method with example
- Recursion in Scala
- Scala | map() method
- Scala Lists
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



