← Back to Scala Course | Chapter 7: OOP Basics | Lesson 1 of 7

Class Basics

In this page:

  1. Class Basics

Class Basics

A Scala class is declared with class ClassName { ... }, bundling data and behavior into a blueprint you instantiate with new. Unlike some languages, a class's primary constructor parameters can be declared directly in the class signature, like class Point(x: Int, y: Int). Every value in Scala, including numbers, is technically an object of some class.

Note: Constructor parameters declared directly in the class signature avoid needing a separate explicit constructor block for the common case.

Example: Class Basics

markup
class Dog(name: String) {
  def bark(): Unit = {
    println(s"$name says Woof!")
  }
}

object Main extends App {
  val rex = new Dog("Rex")
  rex.bark()
}
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.