Inheritance and Overriding in Scala: A Complete Guide
Diagram showing the inheritance hierarchy in Scala with parent and child classes
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.
Inheritance is a mechanism where a child class (subclass) inherits properties and methods from a parent class (superclass).
class ParentClass {
// Parent class properties and methods
}
class ChildClass extends ParentClass {
// Child class can access parent class members
}
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
Dog
extends Animal
, meaning it can access sound()
from Animal
.bark()
.Overriding allows a subclass to provide a specific implementation for a method already defined in the superclass.
class ParentClass {
def methodName(): ReturnType = {
// Default implementation
}
}
class ChildClass extends ParentClass {
override def methodName(): ReturnType = {
// New implementation
}
}
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
sound()
method in Animal
is overridden in Cat
.override
keyword ensures that the method is correctly overridden.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
}
}
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
An abstract class in Scala contains abstract methods that must be overridden in subclasses.
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.
✅ 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.
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!