Get startedGet started for free

Class Inheritance in Object-Oriented Programs

1. Class Inheritance in Object-Oriented Programs

In this video, we're going to introduce the concept of class inheritance in object-oriented programming, including how to implement it in Python and why we might use it. Let's go!

2. Class inheritance in object-oriented programming

Another key concept in object-oriented programming is that of class inheritance. This is the process by which one class can "inherit" properties -- that is, methods and attributes -- from another, "parent" class, and then potentially add specific properties of its own as well. As an example, consider the Dog class we have already been talking about. We could create another class called Poodle that inherited from the general Dog class, and then included specific attributes, such as having curly hair. The Dog class itself could also inherit from a broader Pet class, which could in turn inherit from an Animal class. There is no limit to the number of layers of inheritance, although at some point it may become impractical to implement so many classes, especially if your program is otherwise fairly simple.

3. Class inheritance example

Consider our example of the Dog class from earlier. Now imagine that we want to expand our program to accommodate various types of pets, not all of which are dogs (and not all of which bark!). We could copy-and-paste the logic we have written into several new classes, such as Cat, Horse, Gerbil, etc but it might be nice to reuse some of the logic we've already created, such as the requirement that pets be named when we create them. To do this, we would first define the broader, parent class "Pet" using the same syntax as before. Next, when we define the Dog class, we specify the name of the parent class by placing it inside of the parentheses in the class definition. From there, we can define any Dog-specific methods we want, and all of the logic from within the Pet class will automatically apply to the Dog class as well. So just as before, we can create a new Dog named Lacy and have her bark!

4. Class inheritance example continued

To look at a couple more examples of class inheritance, see here how we can produce more classes that inherit from the "Pet" class but implement different behaviors. The Cat class features a "meow" method and the Horse class features a "neigh" method, but both of them take a name attribute because this was specified in the parent "Pet" class. Notice that when we call the "meow" method on Fluffy the cat, we get back a "Meow!", and when we call "neigh" on Midnight the horse, we get back "Neigh!". We will not, however, be able to make Fluffy neigh or bark, or have Midnight meow, because those methods were only implemented on the individual Horse, Dog, and Cat classes, respectively.

5. Let's practice!

Now that we've talked about class inheritance in object-oriented programming, let's try to implement our own basic object-oriented program!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.