0

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.

2
  • 2
    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 Commented Oct 31, 2022 at 5:09
  • 2
    What you describe has nothing to do with overriding. As said above you want somehow a state and methods that act on it, there are many different ways to do that. Commented Oct 31, 2022 at 6:37

2 Answers 2

2

So, basically you want to define a() to use a dynamic behaviour.

object Behave {

  val helloComputeBehaviour: () => Unit =
    () => {
      // default behaviour
      var hello = "Hello"
    }

  val printDayGreetingBehaviour: () => Unit =
    () => {
      // behaviour after switch
      println("Hi, how is your day going")
    }

  var behaviour: () => Unit =
    helloComputeBehaviour

  def a(): Unit =
    behaviour()

  def b(): Unit = {
    // switch behaviour
    behaviour = printDayGreetingBehaviour
  }


  def c(): Unit = {
    // go back to default behaviour
    behaviour = helloComputeBehaviour
  }

}

Sign up to request clarification or add additional context in comments.

Comments

2

As someone who strongly prefers not to use vars, I do not think the following is elegant, but if vars are your cup of tea, you could do something like this:

class xyz {

  private val resetHello: () => Unit = () => {
    // set hello ...
  }

  private val printHi: () => Unit = () => {
    // print "Hi..."
  }
  
  // variable holding the current behavior of def a()
  private var behaviorOfA: () => Unit = resetHello

  def a(): Unit = {
    // execute the current behavior
    behaviorOfA()
  }

  def b(): Unit = {
    behaviorOfA = printHi
  }

  def c(): Unit = {
    behaviorOfA = resetHello
  }
}

2 Comments

How is that different from the other answer?
It's pretty much the same. (As you can seen from the timestamps, I posted mine 5 minutes before @sarveshseri - I guess we both started typing our replies while the other one's wasn't there.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.