Inheritance

6 minutes
Share the link to this page
Copied
  Completed
To have a superclass is to extend, or inherit from, a class. Thus, to be a subclass is to extend a class. Every class in Java can have only one direct superclass from which it inherits some of its fields and methods. ... From its superclass, a class inherits all fields and methods that are not declared as private.

Transcript

Hello, in this Java programming video, we are going to take a look at inheritance. And it literally in the name it inherits from something. So you might hear terms like you know, Father child, you know, grandfather, father fought while the child in classes, well when you look at different tutorials, and it's very similar to that the way you inherit genes from your parents, and they inherit genes from their parents and so forth, we can do a similar soul jogging heritance. It's not just a bit of a look game with the gene pool with the inheritance in programming, we can pacifically you know, specify what we want to, you know, inherit. So, I'm going to create another class, it could be an animal file, and you could do part of a packet and import it the way we've done that fine. I'll keep it simple.

I'll put it here from the book class. And I'm gonna call this the no so we got enemy class here. And I'm going to call the like so. And, and here what I'm actually going to do in these over here, I'll make this public. I'll get rid of the getter than the setter. I'm going to actually move these here as an enemy so that they'll stay part of here.

And the reduced how. That's a pretty generic method. So we'll put that right here. Okay, so this is given an error, and that's because we haven't inherited from our code class yet. So at the end of the enemy class line is the name of the class character in this case. Aleksey or aerosol disappears.

What does this mean? This means anything that's part of the cars class, and in the methods and its variables will be accessible from the enemy class. Well, my thinking, Okay, why? What the benefit of even doing that I have thought about it. No more code with this last thing. Maybe it's all over the place.

The reason is to look at it lives. How would you tell they're pretty generic properties for let's say, some character, but we have enemy. Maybe we have. Cloud public needs to be defined in its own file. And we could do that then. class.

Last one. Okay, so it's already created a class for me, which is pretty darn awesome. So in there for the x character there we go, Oh, you need to do an import because it's all part of the same package anyway, so it's all good. And obviously the extended character as well. And maybe we have a constructor for this class. So do public.

Enemy. And let's say not maybe by default we set itself like this doc don't need to use this keyword. I'm going to show you You can use it and you can see exactly what the properties, we haven't added the health, we haven't added a live variable, or a would use health method. We haven't you know, any of that, but except to access it because it's inherited it from the character. So this house medical enemy, the house with 1000 by default, so we have differences and this is just a very simple example. So you can have a variety of methods, variety of properties that are unique to each class.

But there's generic stuff. So the character class could be used to you know us from let's say, another class for player and they could inherit from a player could avoid supply could have health they could ever reduce health method and you know, any of the generic properties and wealth so that's, you know, the sort of the main part of inheritance. You have a class likes them and you have the rest of them will just be public classes. If you inherit from you can call this like the father class super class, or you the keyword extends and it gets all of its properties. So if we go to System Java, and that's because we removed it get out and set up a method. Whereas if we just do now run it it's 100.

So let's just import dot. For me. That's crazy, super, super cool as a new enemy. The constructor does not take any parameters. By default, you will set the health to 1000. So it would assist them quintella do Effie.

Again, we go back to our simple enemy. We never created a health variable To the enemy. We never created a health variable or a reduced health method. But we can access all of it. And that's the beauty of inheritance. That's it really feel free to experiment, add methods, add variables, see what you can do with it.

If you have any questions, feel free to reach out and I look forward to seeing you in the next awesome video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.