Scala inheritance tests: private, protected, def, val, and var
As a brief note, here are some tests I just created with Scala 3 to test concepts like private, protected, def, val, and var with inheritance tests, i.e., where classes extend other classes or traits:
// FAILS
// package private_def:
// trait StoreProduct:
// private def id: Int // compiler:
// // "abstract method id may not have `private` modifier"
//
// class Pizza extends StoreProduct:
// val id = 1
package def_val:
trait StoreProduct:
protected def id: Int
class Pizza extends StoreProduct:
val id = 1
package val_val:
trait StoreProduct:
protected val id: Int
class Pizza extends StoreProduct:
val id = 1
package var_var:
trait StoreProduct:
protected var id: Int
class Pizza extends StoreProduct:
var id = 1
// FAILS
// package val_var:
// trait StoreProduct:
// protected val id: Int
//
// class Pizza extends StoreProduct:
// var id = 1 // compiler:
// // "variable id of type Int needs to be a stable, immutable value"
// FAILS
// package var_val:
// trait StoreProduct:
// protected var id: Int
//
// class Pizza extends StoreProduct:
// val id = 1 // compiler:
// // "class Pizza needs to be abstract, since protected
// // var id_=(x$1: Int): Unit in trait StoreProduct in
// // package var_val is not defined"
Hopefully you can see from the comments what works and what doesn’t work when you’re using inheritance in Scala.