title: Object extends Trait, Class extends Trait, both have to implement method
body: <p>I have the following setup:</p>
  
  <pre><code>trait A
  {
    def doSomething(): Unit;
  }
  
  object B extends A
  {
   override def doSomething(): Unit = 
   {
     // Implementation
   }
  }
  
  class B(creator: String) extends A
  {
   override def doSomething(): Unit =
   {
     B.doSomething() // Now this is just completely unnecessary, but the compiler of course insists upon implementing the method
   }
  }
  </code></pre>
  
  <p>Now you may wonder why I even do this, why I let the class extend the trait as well.</p>
  
  <p>The problem is, that somewhere in the Program there is a Collection of A.
  So somewhere:</p>
  
  <pre><code>private val aList: ListBuffer[A] = new ListBuffer[A]
  </code></pre>
  
  <p>and in there, I also have to put Bs (among other derivates, namely C and D)</p>
  
  <p>So I can't just let the B-class not extend it.</p>
  
  <p>As the implementation is the same for all instances, I <strong>want</strong> to use an Object. </p>
  
  <p><strong>But</strong> there is also a reason I really <strong>need</strong> this Object. Because there is a class:</p>
  
  <pre><code>abstract class Worker
  {
   def getAType(): A
  
   def do(): Unit =
   {
     getAType().doSomething()
   }
  }
  
  class WorkerA
  {
    def getAType(): A =
    {
      return B
    }
  }
  </code></pre>
  
  <p>Here the singleton/object of B gets returned. This is needed for the implementation of do() in the Worker.</p>
  
  <p><strong><em>To summarize:</em></strong></p>
  
  <p>The object B is needed because of the generic implementation in do() (Worker-Class) and also because doSomething() never changes.</p>
  
  <p>The class B is needed because in the collection of the BaseType A there are different instances of B with different authors.</p>
  
  <p>As both the object and the class have to implement the trait for above reasons I'm in kind of a dilemma here. I couldn't find a satisfying solution that looks neater.</p>
  
  <p><strong>So, my question is</strong> (It turns out as a non-native-speaker I should've clarified this more)</p>
  
  <p>Is there any way to let a class extend a trait (or class) and say that any abstract-method implementation should be looked up in the object instead of the class, so that I must only implement "doSomething()" (from the trait) once (in the object)? As I said, the trait fulfills two different tasks here.
  One being a BaseType so that the collection can get instances of the class. The other being a contract to ensure the doSomething()-method is there in every object.</p>
  
  <p>So the Object B <em>needs</em> to extend the trait, because a trait is like a Java interface and every (!) Object B (or C, or D) <strong>needs</strong> to have that method. (So the only option I see -> define an interface/trait and make sure the method is there)</p>
  
  <p><strong>edit</strong>: In case anyone wonders. How I really solved the problem: I implemented two traits.</p>
  
  <p>Now for one class (where I need it) I extend both and for the other I only extend one. So I actually never have to implement any method that is not absolutely necessary :)</p>