Dear Rajesh,

Hope you are doing well.

Overriding is only for methods not to override a variable. What changes if you override a variable with another variable of the same type? If anything, the value and that can just be set anytime anyway, because it is a variable

Override in scala is for functions. I will give example for both the scenarios.


 example 1:

trait A {
  var a: Int = _
}

class B (a0: Int) extends A {
  a = a0
}

example2:

case class Student(name: String, var age: Int = 12)

object main extends App {
  val student = Student("priya")
  student.age = 25
  println(s"name: ${student.name}, age: ${student.age}")
}



Override and inheritance example:

class Bank{  
        def getRateOfInterest()={
            0
        }  
    }  
       
    class SBI extends Bank{  
        override def getRateOfInterest()={
         8
        }  
    }  
       
    class ICICI extends Bank{  
        override def getRateOfInterest()={
            7
        }  
    }  
     
    class AXIS extends Bank{  
        override def getRateOfInterest()={
            9
        }  
    }  
       
    object MainObject{  
        def main(args:Array[String]){  
            var s=new SBI();  
            var i=new ICICI();  
            var a=new AXIS();  
            println("SBI Rate of Interest: "+s.getRateOfInterest());  
            println("ICICI Rate of Interest: "+i.getRateOfInterest());  
            println("AXIS Rate of Interest: "+a.getRateOfInterest());  
        }  
    }  

Output:

SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9

image


I hope this will help you.

Please let us know if you have any concern over this.

We will be glad to assist you.


Please note if you are not happy with the response on this ticket, please escalate it to escalations@edureka.in.
We assure you that we will get back to you within 24 hours
 


Regards,
Kamini at Edureka
edureka! Support Team
On Wed, 15 Mar at 7:04 PM , Rapuvvadi <rapuvvadi@gmail.com> wrote:
Hi,

In Module 2 (oops concepts of scala), in slide 18, it is mentioned that super class methods and fields can be override in sub class. Can you give example?

I wrote this and i am getting error says, you can"t override a variable.

class test1 {
var i = 10
def disp = println(i)
}

class test2 extends test1{
override var i = 20
override def disp = println(i)
}

object test extends App{
var c = new test2
}

Error: varibale i cannot override a mutable variable i.

Please let us know your opinion on our support experience.




Awesome Just Okay Not Good
336265