Scala Final – Variable, Method & Class | Scala This
Scala course with real-time projects Start Now!!
In this Scala Programming tutorial, we will talk about two keywords called Scala This and Scala Final. Along with this, we will also discuss an example of Scala Final & Scala This. In Scala Final, we will discuss Scala Final Variables, Methods, and Class with their examples.
So, let’s start Scala Final Tutorial.
What is Scala This & Scala Final?
In Scala, the ‘this’ keyword lets us refer to the current object. ‘final’ prevents a class from deriving members from its superclass. These can be variables, methods, and classes. Let’s first learn about ‘this’.
Let’s discuss Scala Variables with Examples
a. Scala This
When we want to refer to the current object for a class, we use the ‘this’ keyword. Then using the dot operator, we can refer to instance variables, and the class’ methods and constructors.
Let’s take an example.
Example. 1 Scala This-
Let’s see how ‘ Scala This’ works. In the following example, we use ‘Scala This’ to call the primary constructor and the instance variables.
scala> class Marks{ | var midterm=0 | var finals=0 | def this(midterm:Int,finals:Int){ | this() | this.midterm=midterm | this.finals=finals | } | def show(){ | println("You've got "+midterm+" in midterms and "+finals+" in finals”) | } | } defined class Marks scala> var chemistry=new Marks(96,99) chemistry: Marks = Marks@7e3ee128 scala> chemistry.show()
You’ve got 96 in midterms and 99 in finals.
Must read about Scala Case Class | Create Scala Object
Example. 2 Scala This-
scala> class Calculator(a:Int){ | def this(a:Int,b:Int){ | this(a) | println(a+"+"+b+"="+{+b}) | } | } defined class Calculator scala> var c=new Calculator(3,4) 3+4=7 c: Calculator = Calculator@3723de42
Here, we call one constructor from another. This call statement has to be the first statement in this body.
Example. 3 Scala This-
scala> class Calculator(){ | def this(a:Int){ | this() | println(a) | } | } defined class Calculator scala> var c=new Calculator(3) 3 c: Calculator = Calculator@4e8036bb scala> var d=new Calculator() d: Calculator = Calculator@45c3cf0c
Let’s explore Scala String Method with Syntax and Method
b. Scala Final
When we don’t want a class to be able to inherit a member from its superclass, we declare that member final. This member may be a variable, a method, or even a class.
i. Scala Final Variables
When a variable is ‘final’, we cannot override it in the subclass.
scala> class Super{ | final var age=18 | } defined class Super scala> class Sub extends Super{ | override var age=21 | def show(){ | println("age="+age) | } | } <console>:13: error: overriding variable age in class Super of type Int; variable age cannot override final member override var age=21 ^
As you can see, Scala threw an error when we tried to override the final variable.
Let’s Discuss Scala Access Modifiers: Public, Private and Protected Members
ii. Scala Final Methods
Like the Scala final variable, a final method won’t let you override it in the subclass. Let’s watch.
scala> class Super{ | final def show(){ | println("Hello") | } | } defined class Super scala> class Sub extends Super{ | override def show(){ | println("Hi") | } | } <console>:13: error: overriding method show in class Super of type ()Unit; method show cannot override final member override def show(){ ^
Learn Scala Closures with Examples
iii. Scala Final Classes
Finally, when we don’t want a class to be inheritable, we declare it ‘final’. This means another class cannot extend it.
scala> final class Super{ | def show(){ | println("Hello") | } | } defined class Super scala> class Sub extends Super{ | def sayhi(){ | println("Hi") | } | } <console>:12: error: illegal inheritance from final class Super class Sub extends Super{ ^
Let’s explore Scala Functions: Quick and Easy Tutorial
So, this was all about Scala Final tutorial. Hope you like our Explanation on Scala This.
Conclusion
Hence, in this tutorial, we studied “Scala This” lets us refer to the current object and “Scala Final” stops us from overriding a variable or a method and inheriting a class. In addition, we talked about Scala Final Variables, Methods & Classes with their examples. Drop your queries in the comments.
Related Topic- Scala Exceptions and Exception Handling
Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google