1 00:00:00,450 --> 00:00:04,720 Hi in this video we'll learn how to implement classes in Scala. 2 00:00:04,720 --> 00:00:09,330 Like Java, Scala can have multiple constructors which consist of primary and 3 00:00:09,330 --> 00:00:11,090 auxiliary constructors. 4 00:00:11,090 --> 00:00:15,250 Constructors in Scala may be a little different from what you may be used to. 5 00:00:15,250 --> 00:00:19,009 Let's get a better idea on these topics by implementing some examples. 6 00:00:20,450 --> 00:00:24,690 In Scala the primary constructor is blended within the class definition 7 00:00:24,690 --> 00:00:29,540 whereas auxiliary constructors have to be defined using the this keyword. 8 00:00:29,540 --> 00:00:34,150 Unlike Java you don't have to prepend a public key word before defining a class 9 00:00:34,150 --> 00:00:37,710 as Scala classes have public visibility by default. 10 00:00:37,710 --> 00:00:41,500 Let's create a new super hero class within our project. 11 00:00:41,500 --> 00:00:46,220 We'll right click on source, select new Scala class. 12 00:00:51,086 --> 00:00:54,541 If the parameters passed into the class definition of vars, 13 00:00:54,541 --> 00:00:58,406 scala automatically turns them into public fields with getters and 14 00:00:58,406 --> 00:01:01,882 setters which I initialize with a new instance of the class. 15 00:01:01,882 --> 00:01:06,378 If you need a field with read only properties, use a val similarly 16 00:01:06,378 --> 00:01:11,840 to Java if you want to make a field private prepend the private key word. 17 00:01:11,840 --> 00:01:14,992 It is quite straightforward to use these fields in our class, 18 00:01:14,992 --> 00:01:18,994 let's create a details method within our class, which prints out the name and 19 00:01:18,994 --> 00:01:20,585 birthplace of our superhero. 20 00:02:06,528 --> 00:02:10,438 Great, let's create a new incidence of our superhero class in our main object and 21 00:02:10,438 --> 00:02:11,583 check out the results. 22 00:02:45,742 --> 00:02:48,202 Awesome we created our first Calla Class. 23 00:02:48,202 --> 00:02:52,130 Let's continue to create additional constructors for our class. 24 00:02:52,130 --> 00:02:58,290 We'll implement a constructor that takes just the name of the super hero, 25 00:02:58,290 --> 00:03:02,611 and another constructor with name and birth place. 26 00:03:27,229 --> 00:03:30,420 We define additional constructors with the def and this keywords. 27 00:03:30,420 --> 00:03:36,620 Inside our constructor we make a call to the primary constructor using this. 28 00:03:36,620 --> 00:03:40,638 And passing in the values for the parameters of the primary constructor. 29 00:03:40,638 --> 00:03:45,234 Remember an auxillary constructor can either call the primary constructor or 30 00:03:45,234 --> 00:03:48,029 a previously defined auxillary constructor. 31 00:04:15,762 --> 00:04:18,229 Let's go back to our superheroes object, and 32 00:04:18,229 --> 00:04:21,336 make calls to our newly created auxiliary constructors. 33 00:04:49,278 --> 00:04:53,639 Great as we previously mentioned Scala provides getters and setters for vars. 34 00:04:53,639 --> 00:04:57,780 We can rewrite these getters and setters if we ever wish to. 35 00:04:57,780 --> 00:05:00,280 Let's take a look at how to do that. 36 00:05:00,280 --> 00:05:04,310 Within our superhero class we'll create a new field called hero age. 37 00:05:09,060 --> 00:05:11,950 Then we'll rewrite the getter and setter methods for heroAge. 38 00:05:18,965 --> 00:05:22,409 Our getter, which will return heroAge, and our setter. 39 00:05:30,372 --> 00:05:35,612 We'll take a new integer variable which we can use to update the hero's age. 40 00:05:48,961 --> 00:05:52,059 Great, we have rewritten the getter and setter for age respectively. 41 00:05:52,059 --> 00:05:56,540 We would use these functions on any superhero instance. 42 00:05:56,540 --> 00:06:00,200 Let's go back to our main class, create a new instance of superhero and 43 00:06:00,200 --> 00:06:03,497 see how we can use our newly created getters and setters for age. 44 00:06:27,859 --> 00:06:30,763 Well, first, set the Hulk's age to 30. 45 00:06:34,735 --> 00:06:37,587 And we'll use the getter of method to return the Hulk's age. 46 00:06:43,627 --> 00:06:47,532 Awesome, we're able to get back the value that we passed in for Hulk's age. 47 00:06:47,532 --> 00:06:51,230 Scala also has a notion of case classes. 48 00:06:51,230 --> 00:06:53,960 Case classes are typically used with pattern matching and 49 00:06:53,960 --> 00:06:58,120 are very easy to use for matching and storing the contents of a class. 50 00:06:58,120 --> 00:07:01,019 Let's create a case class for power stats of our superheroes. 51 00:07:10,449 --> 00:07:12,479 Let's see how we can use our case class for 52 00:07:12,479 --> 00:07:14,694 pattern matching the contents of our class. 53 00:08:17,926 --> 00:08:21,874 Here we build the findHero function which matches the contents of a class to 54 00:08:21,874 --> 00:08:23,450 a string of super hero names. 55 00:08:23,450 --> 00:08:27,321 To use the class, we would simply call the findHero function, 56 00:08:27,321 --> 00:08:31,058 passing in the PowerStats for the hero that we like to match. 57 00:08:46,040 --> 00:08:49,476 As expected the value hero should contain thor. 58 00:08:57,948 --> 00:08:58,739 Awesome.