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 declaring
- In static block
- In Constructor
Scala
class Shapes
{
final val area : Int = 60
}
class Rectangle extends Shapes
{
override val area : Int = 100
def display()
{
println(area)
}
}
object GFG
{
def main(args : Array[String])
{
var b = new Rectangle()
b.display()
}
}
|
Following 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
class Shapes
{
val height : Int = 0
val width : Int = 0
final def CalArea(){
}
}
class Rectangle extends Shapes
{
override def CalArea()
{
val area : Int = height * width
println(area)
}
}
object GFG
{
def main(args : Array[String])
{
var b = new Rectangle()
b.CalArea()
}
}
|
Following 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
final class Shapes
{
val height : Int = 0
val width : Int = 0
final def CalArea()
{
}
}
class Rectangle extends Shapes
{
override def CalArea()
{
val area : Int = height * width
println(area)
}
}
object GFG
{
def main(args : Array[String])
{
var b = new Rectangle()
b.CalArea()
}
}
|
Following error occurs while running above code
Output :
prog.scala:4: error: illegal inheritance from final class Shapes
class Rectangle extends Shapes{
^
one error found
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
02 Sep, 2021
Like Article
Save Article