January 2018
Beginner
426 pages
10h 4m
English
We use the extend keyword to inherit a class. Let's see our Book example to understand this:
class Book(val title: String){
// data and behaviour for Book
}
class Dictionary(name: String) extends Book(name) {
// data and behaviour for dictionary
}
object BookApp extends App {
val dictionary = new Dictionary("Collins")
println(dictionary.title)
}
The result is as follows:
Collins
We can see that Dictionary inherits from Book or Dictionary and is a subclass of the Book superclass. It's important to know that all classes, regardless of using the extends keyword, explicitly inherit Any in Scala. It means that our Book class inherits Any. Now with the establishment of this relationship, if you try to create an instance of ...
Read now
Unlock full access