All right. Let's talk about a more advanced subject here called
an interface.
Now we're getting into the real nitty gritty of Python as
a coding language.
Interfaces are a cool thing.
A lot of people actually don't know them. But when you're
working with a larger framework, you're going to be using
interfaces without even knowing about it.
So what is an interface?
An interface is literally just a template for your classes.
It's actually just, it's a blueprint for designing your classes.
So they usually have some sort of base implementation, but
they also have a set of rules for you to implement changes
on a new subclass.
So let's go ahead and sort of recreate the example from our
last video where we have 'class Animal', and we have 'def speak():
print("Says a thing")', and then we have 'class Cat', which inherits
from 'Animal', and doesn't need to do anything yet.
And if we do, 'henry = Cat()',
'henry.speak()', "Says a thing".
But what if this is our blueprint?
And on every 'Animal' that we subclass to, this is called a
subclass, and this is the base class.
What if we said on every subclass we needed to change this?
Let's go ahead and force this with a rule by raising an exception,
which is the thing we'll talk about in a few lessons from
now I believe, we're going to raise an exception called
'NotImplementedError',
And we can give this an error that says, "Not implemented
please add an animal sound."
Okay, let's run that.
Let's rerun this cell, rerun this cell, and we get the, 'NotImplementedError',
and that's because 'speak' is going to raise an error for us
when we run it from 'Cat'.
Now we're likely never going to run it from 'Animal' since that's a base class,
but in a subclass, we need this to be overwritten.
And what this basically says is, 'That's cool.
Yeah, there's a 'speak' class, but Hey, if you try to use it,
it's not going to work.
You need to overwrite this on every single subclass".
So that's what we're going to do.
This is now our template saying, "Hey, this needs to be implemented".
So let's implement it.
[no audio]
And this is just going to say "Meow". We'll rerun these cells,
and 'henry' now says, "Meow".
But let's take a look at an example with a 'Dog' class.
We've got a 'Dog', it inherits from 'Animal',
and it's not going to do anything either.
We have a new 'doggo', 'doggo.speak()'.
We're going to see the same implementation raised error
[no audio]
where it says 'Not implemented', and 'Not implemented
please add an animal sound".
And that's again, because 'Dog' is a subclass of 'Animal', and
every subclass, we're saying needs to overwrite this.
So 'def speak()', always take 'self' because it is a method on a class.
'print("Woof woof"). "Woof woof". Rerun those cells and it works for us.
Now let's talk about this in here.
This is a little bit different.
We haven't seen the keyword 'raise' yet, but we will be seeing
that eventually.
And all that does is bubbles up an exception, or an error,
or a specific type of error.
This one comes built-in with Python, called the 'NotImplementedError',
and it looks a lot like a function, although it's not lowercase.
It's camel case, a lot like what our classes have been looking like lately.
So this is actually raising a class, and its parameter, its
first parameter, is an error message.
And in a future lesson, we are actually going to be able
to raise an exception like this.
This is called an exception. So we can raise an exception
and we can catch it so that our program doesn't just totally
die. So, for instance, if I comment that out, save, save,
and we get this 'NotImplementedError' on our 'Dog'. We can
actually catch this error, and work with it instead of having it
error out on us like this,
because moving forward, if we have a program that errors
out, chances are it's going to just totally die.
It's not going to know how to handle the error, and it's
just going to totally quit your program, which is terrible.
You never want that.
And so by raising an exception, we can then catch that exception
and work with it gracefully.
And we'll talk about raising exceptions and catching them
a little bit later in depth.
But what I want to talk about here was, this in particular
is going to force us to always implement the 'speak' method
on any subclass that we use.
So the idea here is to not actually use the 'Animal' class,
except to inherit from other classes, or by other classes
rather. So we would never really use this on its own.
It would really only show up in here.
And this is the rule saying that, "Hey, if anytime you're going to
make that animal speak, you actually have to implement
something on your own. That's our blueprint. That's our guide.
That's a perfect template for creating other classes,
subclasses we call them.
And that, in a nutshell, is a class interface in Python.