29: Abstract Classes. Incomplete On Their Own.
Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Categorie:
What if you were writing a class that you wanted to be a base class? A class designed from the very beginning to bring together other classes with common behavior. What if you also wanted to guarantee that this class could never be created by itself? That it must always exist as a base class for some other class? All you have to do is make your class abstract. Then it can only be instantiated as part of another class that derives from your class. This episode will explain why you might want to do this and how. Because one of the biggest reasons to use inheritance is so you can override a method and provide your own implementation, then it makes sense that a big reason for you to plan for inheritance is so that your methods can be overridden. But what if your base class doesn’t have the ability to provide its own implementation. If you leave the method out, then it’s no longer part of the base class. You want it to be included in the base class so you can still make use of the virtual behavior. The solution is to make the method a pure virtual method and declare that there is no implementation at the base class level. This makes your base class abstract. An abstract class cannot be instantiated, or created, on its own. It must always serve as the base class for one or more other classes. As soon as you have a derived class that fully implements all the pure virtual methods, then that derived class becomes concrete. That’s a class that can be created. When you create a concrete class, you end up creating any abstract classes just like any other base class. Making a class abstract just ensures that it will never exist on its own and must always be part of another derived class. It also gives you the ability to declare methods at the base class level which will be guaranteed to be implemented by at least one of the derived classes. Listen to the full episode for more about abstract classes, or you can also read the full transcript below. Transcript One of the main reasons for using inheritance is so that you can override a base class method and provide your own implementation of that method. We can flip this reason around to say that one of the main reasons to plan for inheritance is to allow a derived class to provide its own implementation. You do this by making one or more of your class methods virtual. A virtual method is one that you provide a default implementation but recognize that other classes might want to change that implementation with their own. Sometimes, there’s no good implementation that you can provide at the base class level. You just don’t have enough specific knowledge about how your base class will be used or each derived class is so different that there’s just not enough common ground to provide a reasonable default implementation. You could just leave this method out of the base class since you have no way to implement it. But that means the method won’t exist in your base class. If you’re working with a group of derived classes and referring to all of them through your base class, then you can’t call this method. It doesn’t exist in the base class. A better approach is to go ahead and declare the method in your base class and make it virtual but just don’t provide an implementation. Can you do this? Yes, but it has a side effect. Your base class is no longer complete and can’t exist on its own anymore. It becomes abstract. This requires a little more than just forgetting to implement a method. If you write your class with several methods and don’t implement one of them, then some languages might not complain at all as long as you never actually try to call your missing method. In order to do this properly, you must declare that the method exists, is virtual, and that it’s purposely being omitted. Different languages will have different ways to specify this intent. Doing this makes the method a pure virtual method or an ab