@@ -12,6 +12,8 @@ import scala.beans.BeanProperty
12
12
* extends keyword is used to subclass a class
13
13
* You can have multiple public classes in one file
14
14
*
15
+ * override keyword is mandatory for method overriding
16
+ *
15
17
*/
16
18
class Employee (@ BeanProperty val firstName : String , @ BeanProperty var lastName : String , @ BeanProperty val title : String ) // This is default constructor
17
19
{
@@ -25,10 +27,23 @@ class Employee (@BeanProperty val firstName:String, @BeanProperty var lastName:S
25
27
// Another constructor
26
28
def this (firstName: String , lastName: String ) = {this (firstName, lastName, " Programmer" ) // if constructor block is multiline, First call must be this
27
29
println(" Multiline constructor block!!" )}
30
+
31
+ def fullName = s " $firstName $lastName"
32
+
33
+ def copy (firstName: String = this .firstName, lastName: String = this .lastName, title: String = this .title): Employee =
34
+ {
35
+ new Employee (firstName,lastName, title)
36
+ }
28
37
}
29
38
30
39
class Department (val name : String )
31
40
32
41
class Manager (firstName: String , lastName: String , title: String , val department : Department ) extends
33
- Employee (firstName, lastName, title)
42
+ Employee (firstName, lastName, title) {
43
+ override def fullName = s " $firstName $lastName, ${department.name}, Manager "
44
+
45
+ override def copy (firstName : String = this .firstName, lastName : String = this .lastName, title : String = this .title): Manager = {
46
+ new Manager (firstName, lastName, title, new Department (" Toys" ))
47
+ }
48
+ }
34
49
0 commit comments