Scala Method Overriding & Field Overriding with Example

Free Scala course with real-time projects Start Now!!

In our last tutorial, we saw How to Implement Scala Abstract Class. In this Scala tutorial, we commit to discussing Scala Method overriding and fields overriding in Scala. Moreover, we will learn examples of Scala Method Overriding and Field Overriding.
So, let’s begin with Scala Method Overriding and Field Overriding.

What is Scala Method Overriding?

Scala overriding method provides your own implementation of it. When a class inherits from another, it may want to modify the definition for a method of the superclass or provide a new version of it. This is the concept of Scala method overriding and we use the ‘override’ modifier to implement this.
Let’s see Scala String Method with Syntax and Method
Technically, Scala method overriding is when a member M of class C matches a non-private member M’ of a class from which C inherits. There are some restrictions:

  • M is not privatecla
  • M’ is not final

Let’s take Scala method overriding example.

Real-Time Examples of Method Overriding in Scala

These are the following Scala method overriding examples, let’s see them one by one:

a. Example 1

In this example, class Student extends class Person. It overrides the method greet() from class Person to its own definition using the Scala ‘override’ keyword.

scala> class Person{
| def greet(){
| println("I'm a person")
| }
| }
defined class Person
scala> class Student extends Person{
| override def greet(){
| println("I'm a student")
| }
| }
defined class Student
scala> var s=new Student()
s: Student = Student@6f1d799
scala> s.greet()

I’m a student
Read About Scala Method Overloading with Example

b. Example 2

Let’s take another Scala method Overriding example.

scala> class Vehicle{
| def getNumberOfWheels()={
| 0
| }
| }
defined class Vehicle
scala> class Car extends Vehicle{
| override def getNumberOfWheels()={
| 4
| }
| }
defined class Car
scala> class Bike extends Vehicle{
| override def getNumberOfWheels()={
| 2
| }
| }
defined class Bike
scala> class Auto extends Vehicle{
| override def getNumberOfWheels()={
| 3
| }
| }
defined class Auto
scala> var c=new Car();
c: Car = Car@4f72078d
scala> c.getNumberOfWheels()
res1: Int = 4
cala> var b=new Bike();
b: Bike = Bike@2d6e09f0
scala> b.getNumberOfWheels()
res2: Int = 2
scala> var a=new Auto()
a: Auto = Auto@bedebe9
scala> a.getNumberOfWheels()
res3: Int = 3

In this example, we define a class Vehicle that defines a method getNumberOfWheels(). Three classes- Car, Bike, and Auto- that inherit from this class. They define their own versions of getNumberOfWheels().

Real-Time Example of Scala Method Overriding

Real-Time Example of Scala Method Overriding

c. Example 3

Without the Scala override keyword or annotation, we get the following error:

scala> class A{
| def greet(){
| print("A")
| }
| }
defined class A
scala> class B extends A{
| def greet(){
| print("B")
| }}
<console>:13: error: overriding method greet in class A of type ()Unit;
method greet needs `override' modifier
def greet(){
^

Let’s Explore Scala Option – 13 Simple Methods to Call Option in Scala

What is Scala Field Overriding?

In Scala, we can also override fields.

a. Example of Field Overriding in Scala

In the following example, we have a superclass Vehicle and a subclass Car. A vehicle has a variable ‘wheels’ with the value 0. Car overrides this and gives it a value of 4. When we call the method show() on an object of class Car, it gives us the value 4.

scala> class Vehicle{
| val wheels=0
| }
defined class Vehicle
scala> class Car extends Vehicle{
| override val wheels=4
| def show(){
| println("The car has "+wheels+" wheels")
| }
| }
defined class Car
scala> var c=new Car()
c: Car = Car@6d512521
scala> c.show()

The car has 4 wheels
Let’s Learn Scala Final – Variable, Method & Class | Scala This

b. What Won’t Work

You can only override val fields; this won’t work with vars. This is because we can only read vals, but we can both read and write vars. Let’s take an example.

scala> class Vehicle{
| var wheels=0
| }
defined class Vehicle
scala> class Car extends Vehicle{
| override var wheels=4
| def show(){
| println("The car has "+wheels+" wheels")
| }
| }
<console>:13: error: overriding variable wheels in class Vehicle of type Int;
variable wheels cannot override a mutable variable
override var wheels=4
^

And then take a look at this:

scala> class Vehicle{
| val wheels=0
| }
defined class Vehicle
scala> class Car extends Vehicle{
| override var wheels=4
| def show(){
| println("The car has "+wheels+" wheels")
| }
| }
<console>:13: error: overriding value wheels in class Vehicle of type Int;
variable wheels needs to be a stable, immutable value
override var wheels=4
^

Let’s Look at Scala Operator in Detail
So, this was all about Scala Method Overriding Tutorial. Hope you like our explanation.

Conclusion

Hence in this tutorial, we have seen that what is Scala Method Overriding with real-time examples. In addition, we learned Scala Field Overriding with example. Furthermore, if you have a query, feel free to ask in the comment tab.
See Also- Scala Variables with Examples
For reference  

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *