← Back to Scala Course | Chapter 8: Inheritance & Traits | Lesson 3 of 7

Abstract Classes

In this page:

  1. Abstract Classes

Abstract Classes

An abstract class cannot be instantiated directly and may declare abstract members (methods or fields with no implementation) that subclasses are required to provide. Abstract classes can still hold constructor parameters and concrete (fully-implemented) methods alongside abstract ones. A class can extend at most one abstract class, the same single-inheritance rule as regular classes.

Warning: Trying to write new Shape() on an abstract Shape class is a compile error.

Example: Abstract Classes

markup
abstract class Shape {
  def area(): Double

  def describe(): Unit = {
    println(s"Area is ${area()}")
  }
}

class Square(side: Double) extends Shape {
  def area(): Double = side * side
}

object Main extends App {
  val sq = new Square(4)
  sq.describe()
}
🔒

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.