In this video, we're going to continue our kind of a worldwind tour through Object Orientated Programming and Development and talk about a term called Inheritance. Now this is one you're going to see and you're going to use quite a bit. This will be a fundamental part of C# development, C# functionality and C Sharp program design. Inheritance is a functionality that happens between our classes that is a direct result of that Classification or breaking functionality into individual classes. Object Inheritance is very, very similar to Human Inheritance. That is the ability of one class to access the functionality of another class and that would be the Parent Class. Think about this, if you have children or if you've ever been a child, let's do it that way okay? If you've ever been a child and if you're listening to me, then you have, then your parent's had certain functionalities that you automatically had access to. Now maybe you didn't know how to drive when you were 8 years old but you inherited kind of that ability to travel around from your parent's. Right. If they had a car and you were their child, it meant you had a car. So if they could drive 60 miles an hour down the highway, to grandma's house. That meant you could travel 60 miles an hour down the highway to grandma's house. The same thing can happen with our classes and our objects, we can create that kind of relationship. So one class can be the Parent Class, another class can be the Child Class and the Child Class retains it's own functionality and it retains the functionality of the Parent. But it also has the ability to extend or even replace some of the functionalities in the Parent Class. So we can take all the characteristics, the methods, the properties, the functionalities of the Parent Class, automatically use those in the Child Class without having to write any code, and then, we can have the Child Class do a little bit extra. Alright. Now Inheritance creates a special relationship between objects called Specialization. Now let's look at an example of Specialization through Inheritance using the classes in our Veterinary Application that we've talked about in a number of videos in the course so far. You remember our Animal Class? Well think about, if we're writing an application about a veterinary office, they're going to deal with a lot of animals. Now an animal, whether it's a dog, a cat, a snake, a lizard, whatever, it's going to have certain characteristics, a name, a color, an owner. It's going to have a lot of different characteristics right, that's going to be general to every animal. So we can put all those in an Animal Class. Then we can create a Dog Class, because there are certain things about dogs that are unique to dogs. They're common to animals but there not unique to cats or spiders or those kinds of things. So we can have the Dog Class inherit from the Animal Class. So any properties, methods the Animal Class has, we can access through the Dog Class, and then we can add special things, for example, dogs have to be treated for Heartworms. Well not all animals do, so we inherit from Animal, we get the name, the weight, the color, the owner's name. Then in the Dog Class we add a method for dealing with Heartworms. Alright. Then we have a Collie Class, which is a Specialization of the Dog Class, now notice the Collie Class is a Specialization of the Dog Class. The Dog Class is a Specialization of the Animal Class. Now the Collie Class has access to all the functionalities and properties of the Animal Class and the Dog Class, but it can also add functionalities that are unique to the Collie. The Collie maybe susceptible to certain types of diseases and so it needs certain treatments that other dogs may not need, and so we can specialize that way. Notice we can specialize on the feline side, on the cats; cats are obviously animals so we can inherit from Animal, they have names, they have colors, they have weights. But certain animals, for example Persians long haired cats, have difficulties with respiratory issues because of bathing themselves and hair balls and all that. And so you can see what's happening here with Specialization. Since we have classified functionalities as far as animal and then more specifically dogs and then more specifically collie, we can use Inheritance to give us a neat interlocking, if you will, of these different functionalities. Getting more specific as we move down the chain of our classes here, and this is called an Object Model. And so if you've ever heard the term, Object Model, this is what they're talking about. Now keep in mind this is exactly how the .NET Framework is built. Okay. You have certain Base Classes that we then, for example, there's a Controls Class and then that Controls Class is inherited by a Button Class okay? So a button is a control but has different functionalities, different aspects say from a List Box or a Drop-down Box and so we can specialize that way. That happens in the .NET Framework and when you start to think about coding in C# in an Object Orientated Environment, you want to do the same thing with your classes there. Build the most general classes, let everybody inherit from that and then specialize on down and what you're doing here, keep in mind, you're encapsulating, you're abstracting things away from everyone. You're proving for Inheritance here and then we'll talk about Overloading which is another aspect of Inheritance in a separate video. But as long as you can get your head around what Inheritance is, when we actually implement it in code later, the light will come on. And you'll say ah, that, that clown talked about that earlier in the course, I'm right there with him. So that's a quick look at Inheritance.
| Course: | Microsoft C# 2010 |
| Author: | Mark Long |
| SKU: | 34306 |
| ISBN: | 978-1-61866-037-4 |
| Release Date: | 2012-03-19 |
| Duration: | 8.5 hrs / 105 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |