Skip to content
... needs to be abstract

... needs to be abstract

DodaTech 3 min read

The “needs to be abstract” error means a concrete class failed to implement all abstract members declared in a trait or abstract class it extends.

What It Means

In Scala, traits and abstract classes define abstract members — methods without a body, val/var declarations without initial values, and type members without definitions. Any concrete class that extends such a trait must provide implementations for all abstract members. If any member is left unimplemented, the class must be declared abstract, and the error tells you exactly which members are missing.

Why It Happens

  • You extended a trait with abstract methods but didn’t implement all of them.
  • You forgot to provide the body for a method defined in a parent trait.
  • An abstract val or var declared in a trait has no concrete value in your class.
  • The trait has an abstract type member that you didn’t define.
  • You extended a sealed trait hierarchy without providing all required implementations.
  • You thought a default implementation existed but the trait member is genuinely abstract.

How to Fix It

1. Implement all abstract methods

trait Logger {
  def log(message: String): Unit // abstract method
}

// ❌ needs to be abstract: class ConsoleLogger
class ConsoleLogger extends Logger {
  // missing log() implementation
}

// ✅ Provide the implementation
class ConsoleLogger extends Logger {
  def log(message: String): Unit = println(message)
}

2. Initialize abstract vals and vars

trait Named {
  val name: String // abstract val
}

// ❌ needs to be abstract: class Person
class Person extends Named

// ✅ Provide the value
class Person extends Named {
  val name: String = "Alice"
}

3. Implement abstract type members

trait Container {
  type Item // abstract type
  def get: Item
}

// ❌ needs to be abstract: class IntContainer
class IntContainer extends Container {
  def get = 42
}

// ✅ Define the abstract type
class IntContainer extends Container {
  type Item = Int
  def get: Item = 42
}

4. Declare the class abstract if intentional

If you intend the class to be extended further and don’t want to implement all members:

// ✅ Declare abstract — subclasses must implement the rest
abstract class BaseLogger extends Logger {
  def format(message: String): String = s"[LOG] $message"
  // log() is still abstract
}

class SimpleLogger extends BaseLogger {
  def log(message: String): Unit = println(format(message))
}

5. Use override for implemented members

trait Greeter {
  def greet(name: String): String
}

// ✅ override keyword is optional in Scala 2 for abstract methods,
//    but explicit is clearer:
class EnglishGreeter extends Greeter {
  override def greet(name: String): String = s"Hello, $name!"
}
Can a class be both abstract and extend a trait?
Yes — abstract classes can extend both concrete classes and traits. The abstract keyword simply tells the compiler you accept that some members are unimplemented. You only need abstract if at least one member lacks an implementation.
What’s the difference between an abstract class and a trait?
A trait can be mixed into multiple classes (like Java interfaces with default methods). An abstract class can have constructor parameters and is a proper superclass. Both can declare abstract members. In modern Scala, traits are preferred unless you need constructor parameters.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro