In the previous video, we got introduced to what inheritance is in Java, so
let's continue on in that discussion now.
That's how we would override a method in a super class.
But let's get back and look at this animal class again.
We notice that we've got this other public method called move, which is a very
generic term because all animals can move in some way, shape, or form.
So lets go back to the dog class and think how would a dog move.
First one would be walk, so public_void walk.
But it would also have run as well.
So what we might actually have,
if we go back to our animal class what might be smart is to accept a parameter,
and the parameter might be the speed perhaps that this animal is moving.
So we can accept a parameter called int_speed,
which will add how fast it is, and
we can put animal is moving at speed.
So now we've got our parameters, so we go back to the dog class.
We might say if the dog class.
Move.
And now, it's gonna talk move,
it's automatically found the method called move, which is in the animal class.
So I can either just call that method directly like this,
or I could put super.move.
Generally speaking, it's better to use move.
Because that then means if you overwrite the move function at a later time for dog,
it's gonna correctly call that move method.
So let me show you what I mean.
So first, I'm gonna call it like this.
So let's just say a walk.
And again, the number doesn't necessarily mean anything.
Let's just assume that it's just a unit of speed.
So I'm gonna say, it's a 5 when they're walking, like so.
And down for running, so running is maybe two times the speed, make it 10.
So if I got back and run this, we put dog.walk, run that.
Dog.walk has been called, animal is moving at 5.
And likewise if we did a dog.run.
Dog.run called, animal is moving at 10.
And just to be clear, what we could probably have done is we could put.
Animal.move called.
Just to be clear which method is being called.
You can't quite see that but basic animal is moving at five or ten,
you can see Animal.move and Animal.move has been called.
So, the reason, getting back to the dog class,
if I leave this at move instead of putting super.move.
The reason I'm gonna do that would be, at some point in the future,
I might decide I need to create something, some characteristics, something unique for
this dog class, for moving.
So, for example, if we go to Overwrite,
we want to overwrite the move method this time.
You will notice I have got the option just to overwrite that, then I select that one.
What we might start to do there is personally would indicate
also dog.move called.
We might decide to do something like MoveLegs,
cuz obviously, a dog use its legs to move.
And maybe, that would be the speed as well.
So it would create a method called moveLegs.
Something like that.
So now, if we run the move, so
you see what I've got now is I've created now a method move in the dog class itself.
So what should happen is this will now be executed first,
the move method because we've actually created that now in this class.
And in the case of walk, I've got super dog move.
And in the case of run, I've got move.
So let's go back and look at our code.
I'm gonna change this first sign to dog.run.
Dog.run called, then dog.move was called.
Dog.moveLegs was called and Animal.move was called.
Getting back to our code, so back in the run method,
you can see we just called the method move.
So what happened was move, because we didn't have the super keyword.
It knew that I can.
I'm gonna take this, whichever move I can find.
If there's a move method in here, I'm going to execute that.
Otherwise, I'm going to execute the super one automatically.
In other words, the move method that's in the animal class.
And that's what happened, in this case.
And just to confirm that, we could also comment that code out,
that last line, and go back and run that again.
We should have commented that out as well.
Try again.
You can see what's happened here, ..run was called, but
now animal.move was called.
Because it couldn't find the method move, because it wasn't there,
it didn't exist in this dog class.
It automatically went a searched and found one in the animal class and executed that.
And again, if I just un-comment that and
run that again, I have to see that in operation.
Now, it's calling Dog.move.
Dog.moveLegs were called Animal.move.
So you're starting to get the idea now, how methods can be called, depending how
you set this up, either in the class you're working in or in the super class.
It really does depend how that's been setup.
So we're going back to our Dog class again.
You remember that in this case Super.move it is called,
so even though we have got a move method set up,
we are actually telling it not to look at the current class.
We are saying super.move which says only execute the move method in
the super class.
So if we go now and actually run that and use the .walk so
I'll comment that out and un-comment that.
So now I've just got the dog.walk and
I'll stop the dog eating just to save a bit of output.
So if I run that now,
see I can say dog.walk was called animal.move was called.
So, even though we've defined the method here called move in the Dog class,
what's happened is we used the super keyword, super.move.
So in that case, what we're saying to Java is don't try and execute the move method.
If there is one in this class, automatically go to the super class.
In other words, the class we're extending from, inheriting from,
which in this case is animal.
So you can see here now that what we've done with the dog class is we've created
a class that is still based on the animal class.
In that, it's got these base characteristics of the name, the brain,
the body, the size, and the weight.
But in addition, it defines some extra fields, eyes, legs, tail, teeth, coat,
that are applicable only to a dog.
And have also defined some behavior, like chew,
walk and run, which are specific to dogs, move legs, which was specific to dogs.
But we have also overwritten some functionality.
In other words, what we have done here is we have created a method
of the same name as the method from the animal class.
But we have overwritten that with our own unique implementation, which has,
in this case, added the a call to the chew method.
So, for the dog chewing mechanism, but then,
it's also calling the super functionality.
In other words, the eat method from the super class, as well.
So, that's certainly optional.
You don't have to call the code in the super class.
But often, you want to do that to initialize that or
to perform that basic functionality in that basic class.
And that's how you do it,
you can separate the functionality that's unique toward dog and the dog class.
But anything that's generic and specific to an animal, in this case,
we've shifted that off to the animal class.
So let's now just do one more example of a class that inherits from animal.
Let's create a class called fish.
So fish is an animal, so we're gonna extend from animal.
We'll call it fish.
So what are some unique characteristics of a fish?
Well, let's go with a couple Int.
Private int gills.
Fish have got gills.
And they've got eyes, so we need to define those.
Private int fins, fin or fins.
So what we'll do now is we'll type extends_animal, and
we're getting that arrow again now because it wants that constructor to be fixed up.
No default constructor, so we need to create a constructor,
and we'll specify those three.
Now, I want you to see what happened when I did that.
Automatically, it's initialized our three parameters, but
it's also initialized the other three parameters.
The other five parameters from our super class, as well.
And if you recall,
we decided previously that we weren't going to define brand and body.
We're gonna do the same.
One, maybe one, if I were to find that again, and
we'll take those out of the constructor.
I'll delete that so there's a little bit more space.
So, very similar to the dog class.
But in this case, we've created a new fish class that inherits from the animal class,
and we've defined some unique characteristics for the fish, namely,
gills, eyes, and fins.
And we could do something similar with methods.
We could create some.
For example, there's a rest method, because fish rest.
They don't necessarily sleep.
Now, let's try rest.
And create another one, swim.
They're moving or swimming instead of walking or running.
And so for swimming, we might have something like private
void moveMuscles, because that's how a fish moves,
combination of moving its muscles, and
it also needs to use its back fin, so void move BackFin.
So therefore, our swim method.
To swim is a concept of moving the muscles.
So we're gonna call moveMuscles, and then, we're going to moveBackFin.
And we wanna put the speed, how fast they are swimming.
So after we're doing that, the net result is the movement,
and we're gonna call super.move(speed).
So that would be one way to model the swimming event.
So the concept of moving for a fish is to swim.
So it moves its muscles and it moves the back fin,
which the net result of that it actually propels itself.
In other words, it's got some velocity.
And we are calling the super method from the animal class to move.
To be able to move that, and to move this fish to propel it in a certain direction.
And rest is a similar thing, I guess,
we could have defined sleep in the animal class.
The reason I didn't do that is fish, generally, don't sleep.
They actually rest, so it's actually not probably appropriate to put a sleep method
or a state in the animal class because not all animals as we have defined necessarily
sleep, at least in that same fashion that we are used to when we use the term sleep.
A dog that would be appropriate to put sleep in here because, of course,
dogs do sleep.
So you can see that you could take this, you can actually extend this out.
We can even have specific types of dogs that extended on from the dog class.
We could have like a Yorkie dog for an for like an or a Labrador.
That has the unique characteristics of a dog.
So you can keep extending on, and on, and on if you want to.
As you'll see,
when we start getting to the stage of this course Start Creating Some Real Apps,
you'll see that you wanna take care to make sure you're still using this
the same way, and you're not making things overly complicated unnecessarily.
But that comes with experience, and
we'll certainly be talking about that more in future videos.
The main thing is to get a good handle on the concept of inheritance and
how we're taking the base characteristics defined.
In this case, in the animal class, then we're extending from or
inheriting from that class to create a new class that inherits those states.
In other words, the fields and also the behaviors, which is the methods,
which we can use in our new class.
And then we can, of course, then add new methods and new fields to that new class,
as well, very, very powerful.
And you'll see that in operation in future videos,
in future apps that we're gonna create in this course.
So, we're gonna end this video here now.
In the next video, it's challenge time.
I'm gonna challenge you now to create some classes similar to what we've done today,
create a base class and extend from that.
In other words, inherit from that class but to do something completely different
to our example that we've been using, which of course is animals.
So, I'll see you in that next video.