Inheritance and Overriding in Scala: A Complete Guide

3/19/2025

Diagram showing the inheritance hierarchy in Scala with parent and child classes

Go Back

Inheritance and Overriding in Scala: A Complete Guide

Introduction

Inheritance is one of the core concepts of Object-Oriented Programming (OOP), allowing a class to derive properties and behaviors from another class. Overriding in Scala enables a subclass to redefine a method from its parent class, providing more specific implementations.

In this guide, we will explore inheritance and method overriding in Scala, along with syntax, examples, and best practices.


Diagram showing the inheritance hierarchy in Scala with parent and child classes

What is Inheritance in Scala?

Inheritance is a mechanism where a child class (subclass) inherits properties and methods from a parent class (superclass).

Syntax of Inheritance

class ParentClass {
  // Parent class properties and methods
}

class ChildClass extends ParentClass {
  // Child class can access parent class members
}

Example: Basic Inheritance in Scala

class Animal {
  def sound(): Unit = {
    println("Animals make sounds")
  }
}

class Dog extends Animal {
  def bark(): Unit = {
    println("Dog barks")
  }
}

object InheritanceExample {
  def main(args: Array[String]): Unit = {
    val dog = new Dog()
    dog.sound()  // Inherited from Animal class
    dog.bark()   // Defined in Dog class
  }
}

Output:

Animals make sounds
Dog barks

Explanation:

  • Dog extends Animal, meaning it can access sound() from Animal.
  • It also defines its own method bark().

What is Method Overriding in Scala?

Overriding allows a subclass to provide a specific implementation for a method already defined in the superclass.

Syntax of Method Overriding

class ParentClass {
  def methodName(): ReturnType = {
    // Default implementation
  }
}

class ChildClass extends ParentClass {
  override def methodName(): ReturnType = {
    // New implementation
  }
}

Example: Method Overriding in Scala

class Animal {
  def sound(): Unit = {
    println("Animals make a sound")
  }
}

class Cat extends Animal {
  override def sound(): Unit = {
    println("Cat meows")
  }
}

object OverridingExample {
  def main(args: Array[String]): Unit = {
    val cat = new Cat()
    cat.sound()  // Calls overridden method in Cat
  }
}

Output:

Cat meows

Explanation:

  • The sound() method in Animal is overridden in Cat.
  • The override keyword ensures that the method is correctly overridden.

Overriding Variables and Constructors in Scala

Overriding Variables

You can also override val variables in Scala.

class Parent {
  val name: String = "Parent"
}

class Child extends Parent {
  override val name: String = "Child"
}

object VariableOverrideExample {
  def main(args: Array[String]): Unit = {
    val obj = new Child()
    println(obj.name) // Output: Child
  }
}

Overriding Constructors

A subclass must call the primary constructor of the superclass.

class Parent(age: Int) {
  println("Parent class constructor")
}

class Child(age: Int, name: String) extends Parent(age) {
  println("Child class constructor")
}

object ConstructorExample {
  def main(args: Array[String]): Unit = {
    val obj = new Child(25, "John")
  }
}

Output:

Parent class constructor
Child class constructor

Abstract Classes and Overriding

An abstract class in Scala contains abstract methods that must be overridden in subclasses.

Example: Using Abstract Class and Overriding

abstract class Vehicle {
  def wheels: Int  // Abstract method
  def description(): Unit = {
    println(s"This vehicle has $wheels wheels.")
  }
}

class Car extends Vehicle {
  override def wheels: Int = 4
}

object AbstractOverrideExample {
  def main(args: Array[String]): Unit = {
    val car = new Car()
    car.description()
  }
}

Output:

This vehicle has 4 wheels.

Advantages of Inheritance and Overriding

  1. Code Reusability – Reuse properties and methods from a parent class.
  2. Extensibility – Modify or extend behavior using method overriding.
  3. Encapsulation – Maintain a hierarchical structure of classes.
  4. Polymorphism – Allows treating child objects as parent objects.

Best Practices for Inheritance and Overriding

✅ Use inheritance only when classes have a true parent-child relationship. ✅ Use the override keyword to avoid accidental method hiding. ✅ Prefer composition over inheritance when possible. ✅ Use sealed classes to restrict class extensions.


Conclusion

Inheritance and method overriding in Scala allow developers to create flexible, reusable, and modular code structures. By effectively using inheritance, overriding, and abstract classes, you can build robust object-oriented applications in Scala.

Start implementing inheritance and method overriding in your Scala projects today to enhance code efficiency and maintainability!