Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
Abstract classes in Scala provide a mechanism for creating classes that cannot be instantiated, but can be subclassed. They can have both abstract (unimplemented) and concrete (implemented) members. Abstract classes are useful when you want to provide a base for other classes but want to prevent the instantiation of the base class itself.
Let's take a look at the key features and how to define and use abstract classes in Scala:
Definition:
An abstract class is defined using the abstract
keyword. It may contain abstract as well as non-abstract methods.
abstract class Animal { def speak: String // Abstract method, no body def eat: String = "Eating" // Concrete method }
Abstract Members:
In Scala, you don't need to use the abstract
keyword for unimplemented methods. If a method doesn't have a body, it's abstract. Note that you also can have abstract fields in Scala.
abstract class Shape { def area: Double // Abstract field def perimeter: Double // Abstract method }
Instantiation: You cannot create an instance of an abstract class.
val animal = new Animal // This will throw a compile-time error
Subclassing: Subclasses of an abstract class must provide definitions for all of its abstract members, unless the subclass is also declared as abstract.
class Dog extends Animal { def speak: String = "Woof" }
If you don��t implement the abstract methods in the subclass, then the subclass also needs to be marked abstract.
Constructor: Abstract classes can have constructors. You can pass parameters to these constructors when creating an instance of a subclass.
abstract class Person(name: String, age: Int) { def displayDetails: String = s"My name is $name and I am $age years old." } class Student(name: String, age: Int, grade: String) extends Person(name, age) { def showGrade: String = s"I am in $grade grade." }
Trait vs Abstract Class:
In Scala, apart from abstract classes, we also have traits
which are similar but have some differences:
In summary, abstract classes in Scala allow for a partial implementation of a class, where concrete subclasses fill in the missing parts. They're especially useful when you want to encapsulate common logic among a set of related classes while ensuring that some methods or fields are implemented by any concrete subclasses.
Scala abstract vs. concrete classes:
// Abstract class abstract class Animal { def sound(): String } // Concrete class class Dog extends Animal { def sound(): String = "Woof" } val dog: Animal = new Dog()
Defining abstract methods in Scala:
abstract class Shape { def area(): Double } class Circle(radius: Double) extends Shape { def area(): Double = Math.PI * radius * radius }
Inheritance with abstract classes in Scala:
abstract class Vehicle { def start(): Unit } class Car extends Vehicle { def start(): Unit = println("Car starting...") }
Abstract classes and traits in Scala:
trait Logger { def log(message: String): Unit } class ConsoleLogger extends Logger { def log(message: String): Unit = println(s"Log: $message") }
Abstract classes vs. interfaces in Scala:
abstract class Writer { def write(message: String): Unit } trait Emitter { def emit(message: String): Unit }
Scala abstract class constructor:
abstract class Shape(val color: String) { def area(): Double } class Circle(radius: Double, color: String) extends Shape(color) { def area(): Double = Math.PI * radius * radius }
Scala sealed abstract classes:
sealed abstract class Result case class Success(message: String) extends Result case class Failure(reason: String) extends Result
Implementing abstract classes in Scala:
abstract class Printer { def print(content: String): Unit } class ConsolePrinter extends Printer { def print(content: String): Unit = println(content) }
Abstract classes and type parameters in Scala:
abstract class Container[T](value: T) { def getValue(): T = value } class Box[T](content: T) extends Container[T](content)
Scala abstract class examples:
Employee
might have methods like calculateSalary
.abstract class Employee(val name: String, val employeeId: Int) { def calculateSalary(): Double } class Manager(name: String, employeeId: Int, val bonus: Double) extends Employee(name, employeeId) { def calculateSalary(): Double = 50000 + bonus }
Abstract classes in Scala hierarchy:
abstract class Shape { def area(): Double } class Circle(radius: Double) extends Shape { def area(): Double = Math.PI * radius * radius } class Square(side: Double) extends Shape { def area(): Double = side * side }