Skip to content

Commit 8d26414

Browse files
authored
Update Employee.scala
Method overriding
1 parent b9d6b9b commit 8d26414

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

4.Classes-Objects/Employee.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import scala.beans.BeanProperty
1212
* extends keyword is used to subclass a class
1313
* You can have multiple public classes in one file
1414
*
15+
* override keyword is mandatory for method overriding
16+
*
1517
*/
1618
class Employee (@BeanProperty val firstName:String, @BeanProperty var lastName:String, @BeanProperty val title:String) //This is default constructor
1719
{
@@ -25,10 +27,23 @@ class Employee (@BeanProperty val firstName:String, @BeanProperty var lastName:S
2527
//Another constructor
2628
def this(firstName:String, lastName:String ) = {this(firstName, lastName, "Programmer") //if constructor block is multiline, First call must be this
2729
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+
}
2837
}
2938

3039
class Department(val name:String)
3140

3241
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+
}
3449

0 commit comments

Comments
 (0)