Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
This next idea of Inheritance is first a great form of code reuse. We can create a new class, but instead of writing it from scratch, we can base it on an existing class. So let's say you start off by defining in your application a Person class with a few attributes--name and address and phone number, and perhaps some simple behavior in it--and then later on you figure out your application will need another class, and this one called Customer. But you realize as you are writing it that this new Customer class is exactly the same as the Person class, and the only difference is it also has a customerNumber.
Now, you didn't want to add that customer number to your Person class, because we're trying to use abstraction. We're trying to only focus on the essentials, and not all of your Person objects will be customers. Now, you could do this by creating a completely separate class, but in an Object-Oriented language, a better way is that we'll create a new class called Customer, and then we say we are going to inherit from the Person class. The phrase we use is Customer inherits from Person, and that means our new Customer class automatically has everything that the Person class has--all its attributes, all its behaviors-- without us having to write any code.
And we just say in our Customer class what we want to add to it, in this case, we would add a customerNumber or another new attribute or add a new method. Now, by convention, if I'm drawing a diagram of Inheritance, I'll use this style of arrow to show it. Now, that's not super important right now. We will get into the diagrams later. But it is just a way of quickly showing that there is a relationship between these two classes and what that relationship is. Now, the term that's most commonly used for this relationship is that the Person class is the Superclass, whereas the new customer class is called the Subclass.
We can also hear this described as the Parent class and the Child class. Now, not only that, but we're not just limited to one of these. We could then create another new class, in this case called Employee, and also inherit from the Person class so that the new Employee class will automatically have everything that the Person class had, but it might add, say, employee ID or a pay grade and perhaps some different behavior. So now, when we're creating objects, we could choose to make Person objects or Customer objects or Employee objects, and the great thing is if I make a change in the Person class, it will automatically filter down and affect the two subclasses.
Now, a few languages like C++ allow you to inherit from more than one Superclass, and they would bring in attributes and behaviors from multiple other classes. This is what's referred to as Multiple Inheritance. But that can get confusing, and it's much more common that even when you're doing inheritance, you only inherit from one Superclass. You have one parent, single inheritance. And that's what's enforced by Java, C#, Objective-C, Ruby, and that's what we'll be using here.
Now, we'll see more about Inheritance later, and I'll show some techniques for when it's best to use it. But one of the best things about Inheritance is not just the time you save in being able to reuse code, but it's what allows us to use the last of our four key terms, Polymorphism.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101631 Viewers
61 Video lessons · 88386 Viewers
71 Video lessons · 72224 Viewers
56 Video lessons · 103973 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.