April 2018
Intermediate to advanced
396 pages
11h 8m
English
If we use inheritance, we would also leak functionality to subclasses that we do not want. Let's look at the following code:
trait DB { def connect(): Unit = { System.out.println("Connected.") } def dropDatabase(): Unit = { System.out.println("Dropping!") } def close(): Unit = { System.out.println("Closed.") }}trait UserDB extends DB { def createUser(username: String): Unit = { connect() try { System.out.println(s"Creating a user: $username") } finally { close() } } def getUser(username: String): Unit = { connect() try { System.out.println(s"Getting a user: $username") } finally { close() } }}trait UserService extends UserDB { def bad(): Unit = { dropDatabase() }}
This could be a real-life scenario. Because ...
Read now
Unlock full access