> For the complete documentation index, see [llms.txt](https://george-jen.gitbook.io/data-science-and-apache-spark/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://george-jen.gitbook.io/data-science-and-apache-spark/scala-trait.md).

# Trait

### Trait

Traits are abstract data types containing certain fields and methods.

```
trait Greeter {
def greet(name: String): Unit =
println("Hello, " + name + "!")
}
class DefaultGreeter extends Greeter
class CustomizableGreeter(prefix: String, postfix: String) 
  extends Greeter {
override def greet(name: String): Unit = {
println(prefix + name + postfix)
}
}
val greeter = new DefaultGreeter()
greeter.greet("Scala developer") // Hello, Scala developer!
val customGreeter = new CustomizableGreeter("How are you, ", "?")
customGreeter.greet("Scala developer") // How are you, Scala developer?
```

methods in trait may be implemented, may not be implemented.  Methods declared, but not implmented in trait must be implmented by classes that extend (inherit) the trait

```
trait prime {
   def isPrime(x: Int): Boolean
}
```

You can create class that extends trait
