Abstract Classes
In this lesson, you will learn about Abstract Classes in Scala. Abstract classes are used to define a common blueprint for related classes while allowing both abstract and implemented members.
They are commonly used when you want to share state, behavior, and constructor logic across multiple classes.
What Is an Abstract Class?
An abstract class is a class that cannot be instantiated directly. It may contain:
- Abstract methods (without implementation)
- Concrete methods (with implementation)
- Fields and constructor parameters
Abstract classes act as a base class for other classes.
Defining an Abstract Class
An abstract class is defined using the abstract keyword.
abstract class Animal {
def sound(): String
}
Here, the sound method is abstract and must be implemented by subclasses.
Extending an Abstract Class
A concrete class extends an abstract class and provides implementations for all abstract members.
class Dog extends Animal {
def sound(): String = "Bark"
}
The Dog class now provides its own implementation of the abstract method.
Abstract Classes with Concrete Methods
Abstract classes can contain fully implemented methods that subclasses inherit.
abstract class Vehicle {
def wheels: Int
def description(): String = {
"This vehicle has " + wheels + " wheels"
}
}
Subclasses only need to implement the abstract members.
class Bike extends Vehicle {
val wheels = 2
}
Constructor Parameters in Abstract Classes
One major advantage of abstract classes over traits is support for constructors.
abstract class Employee(val name: String, val salary: Double) {
def role: String
def details(): String = {
name + " earns " + salary
}
}
Subclasses pass values to the abstract class constructor.
class Manager(name: String, salary: Double)
extends Employee(name, salary) {
def role: String = "Manager"
}
Abstract Fields
Abstract classes can declare fields without assigning values.
abstract class Shape {
val area: Double
}
Concrete classes must define the field.
class Circle(radius: Double) extends Shape {
val area: Double = 3.14 * radius * radius
}
Abstract Class vs Trait
Although abstract classes and traits may look similar, they serve different purposes.
- Abstract classes support constructor parameters
- Traits support multiple inheritance
- Abstract classes can hold mutable state
- Traits are best for reusable behavior
Choosing between them depends on design needs.
When to Use Abstract Classes
Abstract classes are ideal when:
- You need constructor arguments
- You want to define shared state
- You are modeling strong "is-a" relationships
- You want partial implementation
📝 Practice Exercises
Exercise 1
Create an abstract class called Appliance with an abstract method
powerUsage().
Exercise 2
Create a concrete class WashingMachine that extends Appliance.
Exercise 3
Add a constructor parameter to an abstract class and use it in a subclass.
✅ Practice Answers
Answer 1
abstract class Appliance {
def powerUsage(): Int
}
Answer 2
class WashingMachine extends Appliance {
def powerUsage(): Int = 1500
}
Answer 3
abstract class Device(val brand: String)
class Phone(brand: String) extends Device(brand)
What’s Next?
In the next lesson, you will learn about Inheritance in Scala and how classes derive behavior from other classes.