Inheritance in Java Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
inheritance what maybe probably had a test coming up or project due but I just want to say you'll be okay cuz I'm gonna walk you through inheritance in Java if you're new here my name is Alex I'm a Java tutorials just like this every single week so if you're new here and you might be interested in that then please consider subscribing inheritance is one of those big words that comes along with learning Java and since Java is an object-oriented programming language there are a lot of terms that come with it if you're unsure about what object-oriented programming is or you're just like a little refresher I made a video about it recently and you can check that out on the screen now that will get you a really good fundamental on what object-oriented programming is and how it can help you program so let's first start off by going to file new Java project I'll just call it like inherit something like that hit finish and now we have a Java project set up who here's a gamer well it's great I'm glad to hear that you are and I'm excited to let you know that we're starting a gaming mouse company we're gonna be creating gaming mice of all sorts of varieties me and you were a team and in three months we have to come up with five different gaming mice but we also need to write the software for each of the gaming mice it's like cool yeah I'm excited but how do we start that I know how to make a for loop but how is that gonna help me like make a mouse in some program one java solves this problem for us through its nature of object-oriented programming what we can do is we can create objects for different mice and so yeah well we'll try to do that we'll try to create five gaming mice as objects in this project so we'll go to file new class and in this video we're gonna work on three of them just because five is gonna be too many so we'll call it like Mouse one finish and it'll make two more mouths too and mouse 3 so we've got a three Mouse classes here I'm just going to remove the main method that was an accident so they should all be empty classes like this and don't worry we'll get to inheritance in a second but first let's just start making these Mouse objects and start making them pretty cool we'll say one Mouse is like a cool matte black texture you can set the texture equal to match of course it has left and right click so we'll make some methods like left click and right click and for simplicity sake we'll just print out click like that in each of them but mouse one also has another cool feature it's also got some some RG B's on it so we can set a certain color and we can say which color and we'll print out that it's that color let's go on over to Mouse - it can also do left click right click so I'm just going to copy these over and let's say it's special feature that makes it stand out is that it's Bluetooth so we can have a method like connect and we can connect to bluetooth and finally mouse 3 also knows left click right click so a cup of and paste that over here but it's special feature is that it's ambidextrous so we'll actually just have that like maybe as a property here and say boolean ambidextrous equals true something like that so we've got three Mouse's here each slightly different they all know how to left click right click and so repeating this code a lot can be pretty redundant just makes it difficult to keep track of and if our company grew and we had to make a hundred different mice in a year then copying and pasting this a hundred times is not going to make the code any easier to read so we can make as many mice as we want that have different features by making a generic mouse that has the basics like left-click and right-click so to do that we'll just go to a new class again and we'll just call this Mouse and hit finish in our generic Mouse is gonna have everything that all of our mice is gonna have and all of our mice are gonna have left click right click so we can just take that and put it in here so that way it doesn't need to be in Mouse one it doesn't need to be in Mouse two and it does not need to be in Mouse three make sure all of these are saved and let's say also all of our mice are gonna have a scroll wheel so we can do the same thing let's scroll up scrolled up and scroll down in real life these would be implemented like there'd be code in here like if statements and for loops and parameters but it wouldn't be as easy to show in the console here in a second when everything comes together so now we have our generic mouse that can left click right click scroll up and scroll down we can create a ton of other mice based on this and that's called inheritance and to do inheritance is super simple you just go up to the name of your first Mouse and do extends Mouse okay seems pretty easy let's just do that - Mouse - and also Mouse thing now if we created a main method here to see what's all going on and hit a public static void so we can run our code if we create our first mouse mouse one call it m1 equals new Mouse one if we do m1 dot we can see everything that that mouse one can do and we can see that it has a texture and it can also left click right click scroll down and scroll up even though even though when we go to Mouse one we don't see scroll up or scroll down but since it extends Mouse these two key words mean inherit since it inherits from the generic Mouse it knows everything already and that's why it knows the left click it knows right click and you can also scroll down and scroll up and if we run this we'll actually see those methods being called click click Scrolls down scrolled up and this happens to with all of them so if we change this to Mouse to the same exact thing what happened - - - - and there you go but we also have the property specific to Mouse - we see connect the method Connect since Mouse - is bluetooth we can do m2 dot connect like that and now we see that connected message but we can't for example get a texture because there's no texture in Mouse - there's only a texture in Mouse 1 so what this does is it lets us create a generic object and then we can create a bunch of different variations of it and keep track of it a lot simpler and this is basically how everything is made when you think about it the car company for example has a lot of different cars a lot of different models you've ever heard them say a car platform like the new BMW is on this new x7 wide platform the platform is the generic object the BMW 5-series platform can make a luxury version of that car or a sports version of that car a high-performance version of that car but each version of the car can drive or roll the windows down generic things so I hope this was helpful I tried to do it simply but I realized I may have overcomplicated a little bit but let me know if this mouse example was useful for you remember all inheritances is using methods and properties from a generic class and making different versions of them that's why you see if the examples you have in school or like animal a cat object extends animal a dog object extends animal but then under dog you can have like cocker spaniel Chihuahua Chihuahua extends dog dog extends animal the Chihuahua is gonna have all the generic properties of an animal like a breathe eat things like that so again good luck on your good luck on your test or your project if you're still stuck you can look at videos on my channel I have all like maybe close to 40 now Java tutorials that may be able to help you on your project and prepare you for your tests I know I've helped a lot of people already I know a lot of people have said that my videos if it's helpful to them so they might be helpful to you too good luck out there have a great day [Music]
Info
Channel: Alex Lee
Views: 148,045
Rating: 4.9658713 out of 5
Keywords: inheritance in java, java inheritance, inheritance in java tutorial, inheritance java, inheritance java thenewboston, how to use inheritance in java, java inheritance tutorial, java inheritance example code, java inheritance example, object oriented programming, object oriented programming in java
Id: zbVAU7lK25Q
Channel Id: undefined
Length: 10min 33sec (633 seconds)
Published: Thu Aug 29 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.