I wanted to know if theres a way to override a method within the same class in scala.
class xyz {
def a() : Unit = {
var hello = "Hello"
}
def b() : Unit = {
//method to override the functionality of b, for example lets say I want it to just print "Hi, how is your day going" until its somehow reset and after its resett it should go back to doing var hello = "Hello"
}
}
def c() : Unit = {
//reset a to do what it was doing earlier (var hello = "Hello")
}
Basically I want to compute var hello = "Hello" whenever a() is called until b() is called and then a() should print "Hi, how is your day going" until its reset when c() is called and then it should go back to performing var hello = "Hello". Is there a way to use this, if not is there another way? I don't want to use conditionals. Thanks in advance.
I don't want to use conditionals, then no. What you are describing is essentially a form of state machine and you need to check the state to do it. You can use pattern matching alternatively but it's really gonna be the same thing. Or instead of breaking referencial transparency like this, have different classes with different behavior and return an instance as the next state