What is inheritance?
Video: What is inheritance?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.
Viewers: in countries Watching now:
Most modern programming languages, such as Java, C#, Ruby, and Python, are object-oriented languages, which help group individual bits of code into a complex and coherent application. However, object-orientation itself is not a language; it's simply a set of ideas and concepts.
Let Simon Allardice introduce you to the terms—words like abstraction, inheritance, polymorphism, subclass—and guide you through defining your requirements and identifying use cases for your program. The course also covers creating conceptual models of your program with design patterns, class and sequence diagrams, and unified modeling language (UML) tools, and then shows how to convert the diagrams into code.
- Why use object-oriented design (OOD)?
- Pinpointing use cases, actors, and scenarios
- Identifying class responsibilities and relationships
- Creating class diagrams
- Using abstract classes
- Working with inheritance
- Creating advanced UML diagrams
- Understanding object-oriented design principles
What is inheritance?
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.
There are currently no FAQs about Foundations of Programming: Object-Oriented Design.