>> It wouldn't be much of an object-oriented programming language.
If we didn't support inheritance.
Now, keeping with our theme,
we aren't going to get real deep into exactly what inheritance is,
but instead I want to focus in on the concept,
that we could actually spend
hours talking about what inheritance is.
I'm going to summarize all of this in one slide.
We'll link to some additional resources,
if you wanted to dig deeper into exactly what inheritance is.
What inheritance is, is it's a generalization specialization.
That what I'm going to do,
is create an is-a relationship.
So if we take a look at the first example of,
Student is a Person.
Person is very general.
That's you, that's me,
we're all people, we're all persons.
Student is going to be a specific type of person.
So it's a specialized person.
Same thing then if we bring this down into a little bit more of
a programming construct where we've got
SQL connection and MySQL connection.
In both of those cases,
those could be types of database connection.
The great part about creating
a generalization specialization relationship,
is the fact that I can then count on the generalized code,
maybe things like talking to the network,
and otherwise all being in that general class.
Then, I can add on
whatever specialization code it is that I might need.
I'm going to differentiate this from composition,
which creates a has-a relationship.
So a student has a class, for example,
so they attend the classes,
or a database connection has a connection string.
So that connection string might have the name of the server,
username, password, information, etc.
So has-a relationship, done with properties,
my is-a relationship, that's inheritance.
So let's talk a little bit about this in action.
One thing that we want to highlight right away,
is the fact that all methods in Python are virtual.
Which means that we're always going to be able to override them,
we're always going to be able to redefine their behavior.
We're going to notice that we've got the keyword
super to be able to access the parent class.
We are going to need to do this in the constructor.
Notice that bottom low line there,
must always call the parent constructor.
We're also going to notice that if there's
anything that's going on in that super class,
some like field or a property,
or maybe even a method.
Then, I might want from that super class,
then I can go access that by using that super keyword.
Let's see a real quick little code example here.
You're going to notice that just like before,
I've defined a class called person.
Now, I didn't do anything really fancy here.
I went ahead and set up
the constructor just like we had last time,
added in the little method just like we had last time.
That's it. All this code we've already seen.
Down below is where I'm going to inherit.
There's a few things that I want to highlight in here.
First of all, in order
to indicate that you are going to inherit from a class,
you indicate that parent class
in parentheses right after the name of the class.
So now, I've got my is-a relationship,
Student is a Person.
Now, what you're going to notice,
is that I've got my constructor here.
On my constructor, I'm going to go ahead
and specify all my parameters.
So self just like before.
Then, what I've got is the name,
and in my case, the school.
Now, name actually comes from person.
One of the things that Python insist sign,
is when you derive from a class that you are going to need
to call the parent constructor
if there's anything there that needs to be able to be set up.
So in my case,
I need to set up that name.
So what you're going to notice,
is that I'm going to call super right here.
Then, I'm going to call that constructor by saying __init__,
that's going to allow me to call the constructor,
and then I can pass that name up there.
So whatever name it is that you give to the student,
that's actually going to be passed up to the person
to give me the ability to set that property up.
Then, anything that's specific to student,
in my case, the school,
will go ahead and set up,
and then I can add on additional functionality.
So you'll notice that if I decided to go ahead and use this,
so I set up a brand new student,
passed in the name, passed in the school, I said,
"Say hello", I said, "Sing the song".
We would notice that it would then go
ahead and print out, "Hello, Christopher".
Then, the song of Ode to UMD,
University of Minnesota Duluth,
by the way, not Maryland.
The other thing that you're going to notice with classes,
is I can do a little bit of a, what are you?
So I can say, "Hey,
is this in instance of additional classes?"
So here's that little variable that I've got.
I can say, "Hey,
are you a student?"
Well, we know that it is,
so it's going to return true.
We would expect that.
You're also going to notice that we can say,
"Hey, are you a person?"
Well, remember with inheritance,
we're creating an is-a relationship,
Student is a Person.
So this is actually going to return true.
Last but not least,
I can explore the hierarchy.
So you'll notice that I can say, is subclass,
is student, a subclass of
person or another words, does it inherit?
The answer to that question is, yes.
So if we break this down here, what's going on,
is there is the say hello,
there is the sing the school song.
The other ones down below here.
Let me just real quick arrow to each one of those here.
The top one is,
is this a student?
It's true because it is.
The next one here is,
is this a person?
Again, that is true because we inherited from it.
Then, the last but not least is, is this,
is a student, a subclass of person?
The answer to that is, yes,
because of the fact that we inherit.
Now, it's also worth noting the fact that in Python,
every single class that you
create automatically inherits from object,
object being that base inside of Python.
Now, because of the fact that we all inherit from
the same base and that base has some functionality,
we can always override that behavior.
So going back to our prior example,
if we maybe wanted to override, say hello.
So for our student, maybe it says,
"I am a tired student named."
Then whatever the name is,
we can go ahead and do that.
We can also override those functions that
we wind up getting from that core parent.
You'll notice and we'll link to the docs inside of
the GitHub repository the fact that we have a lot
of really neat functions that are going to
enable some really nice functionality.
For example, one of the most common ones, is this __str__.
What this does is,
it can now automatically convert this to a string.
So anywhere where we need a string,
this can now become a string.
So you'll notice that what we've done here,
is we've now defined or really overridden that __string.
When we do that,
and we now go ahead and say,
"Print presenter", it's now going to print out Christopher.
The reason that it's able to print out Christopher,
is because we overrode that __str.
That's basically our two string.
So now, Python is going to know how to convert
an instance of our object into a string.
So in a nutshell here,
inheritance, it's going to create that is-a relationship.
We indicate what it is that we want to inherit from by just simply
going ahead and putting the parent class inside of parentheses.
We can always override functions,
including the ones from that base object.
You'll notice that we can always add on our functionality.
If there's anything from that parent class that we need,
we can go grab that from super.
Let's go in and take a look at
a demo to help bring all of this together.