Inheritance
In this lesson, you will learn about Inheritance in Scala. Inheritance allows one class to reuse and extend the behavior of another class.
It helps reduce code duplication and enables you to build class hierarchies that model real-world relationships.
What Is Inheritance?
Inheritance is an object-oriented concept where a class (child class) derives properties and behavior from another class (parent class).
In Scala:
- The parent class is called the superclass
- The child class is called the subclass
- The keyword
extendsis used to inherit
Basic Inheritance Example
Below is a simple example of inheritance.
class Animal {
def eat(): String = "Animal is eating"
}
The Animal class defines a common behavior.
class Dog extends Animal {
def bark(): String = "Dog is barking"
}
The Dog class inherits the eat() method from Animal.
Using Inherited Methods
A subclass can use methods defined in the superclass without redefining them.
val dog = new Dog()
dog.eat()
dog.bark()
This demonstrates code reuse through inheritance.
Method Overriding
A subclass can provide its own implementation of a superclass method. This is called method overriding.
The override keyword must be used.
class Cat extends Animal {
override def eat(): String = "Cat is eating fish"
}
Here, the Cat class changes the behavior of eat().
Calling Superclass Methods
You can call the parent class implementation using the super keyword.
class Cow extends Animal {
override def eat(): String = super.eat() + " grass"
}
This allows extending behavior instead of replacing it.
Inheritance with Constructors
When a superclass has constructor parameters, subclasses must pass values to it.
class Person(val name: String)
class Student(name: String, val grade: Int)
extends Person(name)
The Student class inherits the name field from Person.
Protected Members
Scala allows you to control visibility using access modifiers.
public– accessible everywhere (default)protected– accessible in subclassesprivate– accessible only in the same class
class Account {
protected var balance: Double = 0.0
}
class SavingsAccount extends Account {
def deposit(amount: Double): Unit = {
balance += amount
}
}
Single Inheritance in Scala
Scala supports single inheritance for classes. A class can extend only one superclass.
However, Scala supports multiple inheritance using traits, which you will learn later.
When to Use Inheritance
Inheritance is useful when:
- There is a strong "is-a" relationship
- You want to reuse common behavior
- You need polymorphic behavior
Avoid deep inheritance trees to keep code simple and maintainable.
📝 Practice Exercises
Exercise 1
Create a base class Employee with a method getRole().
Exercise 2
Create a subclass Developer that extends Employee
and overrides getRole().
Exercise 3
Use the super keyword inside an overridden method.
✅ Practice Answers
Answer 1
class Employee {
def getRole(): String = "Employee"
}
Answer 2
class Developer extends Employee {
override def getRole(): String = "Developer"
}
Answer 3
class SeniorDeveloper extends Employee {
override def getRole(): String =
super.getRole() + " - Senior"
}
What’s Next?
In the next lesson, you will learn about Generics in Scala and how to write flexible, reusable, and type-safe code.