title: Dynamic polymorphism in Scala
body: <p>I need to change an object's behavior on run-time.<br>
  I have two concrete object (singleton) classes <code>Dog</code> and <code>Cat</code>, both extend from an abstract class <code>Animal</code>.<br>
  The object <code>animal</code> of <code>Animal</code> type should be changed to either <code>Dog</code> or <code>Cat</code> on runtime.<br>
  Here's the rough code of something that I am trying to accomplish:</p>
  
  <pre><code>abstract class Animal() {
      def sound: Unit
  }
  
  object Cat extends Animal {
      def sound: Unit = {
          print("meow")
      }
  }
  
  object Dog extends Animal {
      def sound: Unit = {
          print("bark")
      }
  }
  
  object MyProgram {
      val animal: Animal = _
  
      def initialize(config: Config): Unit ={
          // check the config
          if (config.getString("ANIMAL_TYPE").equals("Dog")) {
              animal = Dog
          } else {
              animal = Cat
          }
  
      }
  
      def run: Unit = {
          animal.sound
      }
  }
  
  def main(): Unit = {
      val config = ConfigFactory.praseFile(myconfigfile)
      MyProgram.initialize(config)
      MyProgram.run
  }
  </code></pre>
  
  <p>Can something like this be done in Scala? If not, how can I accomplish this in Scala. </p>