1 00:00:00,410 --> 00:00:05,334 Creating and using abstract classes can feel a little abstract when you're first 2 00:00:05,334 --> 00:00:06,700 learning about them. 3 00:00:06,700 --> 00:00:10,224 The following code may be a bit confusing at first, but 4 00:00:10,224 --> 00:00:12,810 work through the example with me. 5 00:00:12,810 --> 00:00:18,080 And then if you still have questions when we're done, try watching this video again. 6 00:00:18,080 --> 00:00:21,370 Or reach out for help from the Treehouse community. 7 00:00:21,370 --> 00:00:23,430 Let's start in the collection class. 8 00:00:23,430 --> 00:00:30,130 To make any class an abstract class, you simply add the word abstract in front. 9 00:00:30,130 --> 00:00:35,490 Now, in index.php, let's try to instantiate and object from this class. 10 00:00:40,519 --> 00:00:44,459 New Collection. 11 00:00:48,481 --> 00:00:50,557 By adding the keyword abstract, 12 00:00:50,557 --> 00:00:54,210 you prevent the object from instantiating this class. 13 00:00:54,210 --> 00:00:58,060 That's good for our collection, because our collection class uses the entity 14 00:00:58,060 --> 00:01:01,260 property which is not set in the collection class. 15 00:01:03,550 --> 00:01:04,510 Let's remove this line. 16 00:01:05,510 --> 00:01:07,230 Now, let's go into post. 17 00:01:07,230 --> 00:01:10,580 This class extends the collection class, and 18 00:01:10,580 --> 00:01:13,270 overrides the constructor to set the entity. 19 00:01:13,270 --> 00:01:16,820 There's no easy way to require that the property is set. 20 00:01:16,820 --> 00:01:22,050 Instead, we can require that a method is defined using abstract method. 21 00:01:22,050 --> 00:01:26,550 Instead of relying on a child class to override the constructor, 22 00:01:26,550 --> 00:01:29,120 we can have the constructor call the set entity method. 23 00:01:31,660 --> 00:01:38,120 Right after we set the repo, we'll call $this->setEntity();. 24 00:01:38,120 --> 00:01:41,151 Like with an interface, we can define abstract 25 00:01:41,151 --> 00:01:45,330 methods to guarantee that these methods be defined in the child. 26 00:01:52,820 --> 00:01:54,410 Let's preview in the browser. 27 00:01:55,785 --> 00:02:00,840 Non-abstract method setEntity must contain a body. 28 00:02:00,840 --> 00:02:03,990 Because all interface methods are abstract, 29 00:02:03,990 --> 00:02:08,090 we don't have to use an abstract keyword when defining our method. 30 00:02:08,090 --> 00:02:09,800 Within an abstract class, 31 00:02:09,800 --> 00:02:13,307 all abstract methods must start with a keyword abstract. 32 00:02:15,940 --> 00:02:20,680 Although interfaces are only allowed to specify public methods. 33 00:02:20,680 --> 00:02:24,661 Abstract classes are allowed to specify both public and 34 00:02:24,661 --> 00:02:28,490 protected methods, but not private methods. 35 00:02:28,490 --> 00:02:32,620 If we try to define an abstract method as private, we'll get another error. 36 00:02:34,810 --> 00:02:40,180 Both private and protected methods are restricted to internal access only. 37 00:02:40,180 --> 00:02:44,970 However, private methods cannot be overridden by a child class. 38 00:02:44,970 --> 00:02:50,900 Because abstract methods must be defined in a child, this is a forced override. 39 00:02:50,900 --> 00:02:54,760 We cannot use private access because it cannot be overridden. 40 00:02:56,000 --> 00:02:58,300 Instead, we can use protected. 41 00:02:59,580 --> 00:03:03,420 Protected methods can be overridden by a child class, 42 00:03:03,420 --> 00:03:07,500 but they are restricted to internal access only. 43 00:03:07,500 --> 00:03:12,355 This is exactly what we want for our internal setEntity method. 44 00:03:12,355 --> 00:03:14,610 Were ready to update our post class. 45 00:03:14,610 --> 00:03:19,810 Extending an abstract class works exactly the same way as extending any other class. 46 00:03:19,810 --> 00:03:22,210 So we don't need to change anything there. 47 00:03:22,210 --> 00:03:26,000 However, since we have added another abstract method, we 48 00:03:26,000 --> 00:03:31,170 do need to make sure that our post class implements this new setEntity method. 49 00:03:31,170 --> 00:03:36,420 Instead of extending the constructor, we make this the setEntity method. 50 00:03:36,420 --> 00:03:37,700 Let's preview our site again. 51 00:03:38,870 --> 00:03:42,080 Great, we've cleaned up our errors and everything is working again. 52 00:03:42,080 --> 00:03:47,090 Now, let's take a closer look at combining interfaces and abstract classes.