Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, you can extend a class to create a subclass, which inherits the members of the superclass. This is a fundamental concept in object-oriented programming.
Here's how to extend a class in Scala:
First, let's define a simple class:
class Animal { def speak(): Unit = { println("Some sound") } }
This Animal
class has a method named speak
that prints "Some sound".
To extend the Animal
class, you use the extends
keyword:
class Dog extends Animal { override def speak(): Unit = { println("Woof!") } }
The Dog
class is now a subclass of Animal
. We've also overridden the speak
method to provide a new implementation specific to Dog
.
Once you've defined your subclass, you can create instances of it and use its methods:
val myDog = new Dog() myDog.speak() // Prints: Woof!
super
:In the subclass, you can use the super
keyword to refer to the superclass's members:
class Cat extends Animal { override def speak(): Unit = { super.speak() println("Meow!") } }
If you create an instance of Cat
and call its speak
method:
val myCat = new Cat() myCat.speak() // Outputs: // Some sound // Meow!
Constructors: Subclasses can also call the superclass's constructor using the super
keyword.
Final Classes and Methods: If you don't want a class to be extended, you can mark it as final
. Similarly, if you don't want a method to be overridden in subclasses, you can mark that method as final
.
Abstract Classes: In Scala, you can also have abstract classes which cannot be instantiated directly but can be extended. Abstract classes can contain abstract members (without implementations).
Traits: In addition to class inheritance, Scala also supports trait mixin, allowing for a form of multiple inheritance. Traits are a powerful way to compose behaviors.
Inheritance in Scala works very much like in other object-oriented languages but offers more flexibility and power, especially when combined with traits.
Inheritance in Scala:
class Animal(val name: String) { def speak(): Unit = println(s"$name makes a sound") }
class Dog(name: String, val breed: String) extends Animal(name) { override def speak(): Unit = println(s"$name barks loudly") }
Creating subclasses in Scala:
extends
keyword.class Cat(name: String, val color: String) extends Animal(name) { override def speak(): Unit = println(s"$name meows softly") }
Scala extends keyword usage:
extends
keyword is used to indicate that a class is inheriting from another class.class Bird(name: String, val species: String) extends Animal(name) { override def speak(): Unit = println(s"$name chirps loudly") }
Overriding methods in extended classes:
override
keyword.class Horse(name: String, val color: String) extends Animal(name) { override def speak(): Unit = println(s"$name neighs loudly") }
Multiple inheritance in Scala:
trait Swimmer { def swim(): Unit = println("Swimming") } class Dolphin(name: String) extends Animal(name) with Swimmer
Using super keyword in Scala subclasses:
super
keyword is used to refer to the superclass, allowing access to its methods and properties.class FlyingBird(name: String, val species: String) extends Bird(name, species) { def fly(): Unit = { super.speak() println(s"$name soars through the sky") } }
Scala abstract classes and class extension:
abstract class Shape { def area(): Double }
class Circle(radius: Double) extends Shape { override def area(): Double = math.Pi * radius * radius }
Polymorphism in Scala classes:
val dog: Animal = new Dog("Buddy", "Golden Retriever") val cat: Animal = new Cat("Whiskers", "Gray") val horse: Animal = new Horse("Spirit", "Brown") val animals: List[Animal] = List(dog, cat, horse) animals.foreach(animal => animal.speak())
Mixins and traits in Scala:
trait Jumper { def jump(): Unit = println("Jumping") } class Kangaroo(name: String) extends Animal(name) with Jumper
Initialization order in Scala subclasses:
class Fish(name: String) extends Animal(name) with Swimmer { println(s"Fish class initialized for $name") }
Scala sealed classes and class extension:
sealed abstract class Vehicle case class Car(model: String) extends Vehicle case class Bike(brand: String) extends Vehicle
Scala case classes and class extension:
case class Point(x: Double, y: Double) val origin: Point = Point(0.0, 0.0)