Abstract Methods and Classes3:48 with Alena Holligan
Creating and using abstract classes can feel a little abstract when you're first learning about them. We'll step through defining an abstract class with it's requirements and the errors you may encounter.
Creating and using abstract classes can feel a little abstract when you're first 0:00 learning about them. 0:05 The following code may be a bit confusing at first, but 0:06 work through the example with me. 0:10 And then if you still have questions when we're done, try watching this video again. 0:12 Or reach out for help from the Treehouse community. 0:18 Let's start in the collection class. 0:21 To make any class an abstract class, you simply add the word abstract in front. 0:23 Now, in index.php, let's try to instantiate and object from this class. 0:30 New Collection. 0:40 By adding the keyword abstract, 0:48 you prevent the object from instantiating this class. 0:50 That's good for our collection, because our collection class uses the entity 0:54 property which is not set in the collection class. 0:58 Let's remove this line. 1:03 Now, let's go into post. 1:05 This class extends the collection class, and 1:07 overrides the constructor to set the entity. 1:10 There's no easy way to require that the property is set. 1:13 Instead, we can require that a method is defined using abstract method. 1:16 Instead of relying on a child class to override the constructor, 1:22 we can have the constructor call the set entity method. 1:26 Right after we set the repo, we'll call $this->setEntity();. 1:31 Like with an interface, we can define abstract 1:38 methods to guarantee that these methods be defined in the child. 1:41 Let's preview in the browser. 1:52 Non-abstract method setEntity must contain a body. 1:55 Because all interface methods are abstract, 2:00 we don't have to use an abstract keyword when defining our method. 2:03 Within an abstract class, 2:08 all abstract methods must start with a keyword abstract. 2:09 Although interfaces are only allowed to specify public methods. 2:15 Abstract classes are allowed to specify both public and 2:20 protected methods, but not private methods. 2:24 If we try to define an abstract method as private, we'll get another error. 2:28 Both private and protected methods are restricted to internal access only. 2:34 However, private methods cannot be overridden by a child class. 2:40 Because abstract methods must be defined in a child, this is a forced override. 2:44 We cannot use private access because it cannot be overridden. 2:50 Instead, we can use protected. 2:56 Protected methods can be overridden by a child class, 2:59 but they are restricted to internal access only. 3:03 This is exactly what we want for our internal setEntity method. 3:07 Were ready to update our post class. 3:12 Extending an abstract class works exactly the same way as extending any other class. 3:14 So we don't need to change anything there. 3:19 However, since we have added another abstract method, we 3:22 do need to make sure that our post class implements this new setEntity method. 3:26 Instead of extending the constructor, we make this the setEntity method. 3:31 Let's preview our site again. 3:36 Great, we've cleaned up our errors and everything is working again. 3:38 Now, let's take a closer look at combining interfaces and abstract classes. 3:42
You need to sign up for Treehouse in order to download course files.Sign up