Inheritance (extends) | Java | Tutorial 34

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to draft Academy my name is Mike in this tutorial I want to talk to you guys about inheritance specifically we're gonna be looking at the extends keyword in Java we're gonna talk about how we can extend the functionality of a class into another class this is a really cool feature in Java and when you're creating a lot of classes and you're using different class structures in Java this can come in handy a lot so I'm gonna show you guys an example we're gonna talk about what this is and why it's awesome so over here in my files I've created a file called chef dot Java and this is a class that's just called chef and inside of this class we basically have three methods so we have this make chicken method we have this make salad method and we have this make special dish method so these are three methods that would correspond to a chef right a chef can do let's say our chef can do three things you make chicken he can make salad and you can make a special dish and inside of each one of these methods we're basically just printing out some text so these are really simple methods but they're just here to kind of illustrate the point I'm trying to make so a for example and make chicken it's just system dot print line it says the chef makes a delicious chicken and make salad it just says the chef makes a healthy salad and make special dish it just says the chef makes barbecue ribs so here's our basic chef class you know the chef can do three things make chicken mix out or make a special dish over here in my app dot Java file and I have my main method over here I've actually created a chef so it's just called normal chef and I set it equal to a new chef all right so I just created a new instance of the chef class I created a chef object and what I can do now is I can actually perform some of those operations so I can say hey normal chef why don't you make chicken so when I call this make chicken method you'll see over here on the screen we should print out the chef makes a delicious chicken so I told the chef to make a chicken and he did so then I can also say chef dot make salad and the chef will make a salad so let's press play and you can see it says the chef makes a healthy salad I can also make the special dish normal chef make special dish and this should make the barbecue ribs so our normal chef can make a variety of dishes now let's say that we wanted to model another type of chef in our program so let's say that in addition to just creating a normal sort of generic chef we also wanted to create an Italian chef well I can come over here and I'm just gonna make a new class so I'll make a class called Italian chef and we can just click finish and this is gonna create an Italian chef class for us so you can see here we have this Italian chef Java file and we have this public class Italian chef so now we're dealing with another chef right so let's say that our Italian chef can cook everything that the normal chef could cook so our normal chef can cook chicken salad and a special dish and let's say that our Italian chef can also cook chicken salad and a special dish but in addition to those three dishes the Italian chef can also cook spaghetti so one thing I can do is just I can come over here and I can just write out all of those methods again right so I could come to this chef class and we'll copy all of these methods because remember the Italian chef can do everything the normal chef can do so I'll copy all these methods I'll put them in over here so now the Italian chef is able to make solid chicken and a special dish and he's able to make one more thing which is spaghetti so we'll add another method public void make pasta and in here it's just gonna basically print out like the chef makes pasta so here we have a method and the chef is now able to make pasta but let's also say that this chef wants to have a different special dish so that normal chef was able to make barbecue ribs as the special dish but let's say that the Italian chef is gonna make eggplant parm so so now when we use this special dish the chef is going to make eggplant parm so head back over to our app dot Java file and I'm just going to copy this so we'll make another chef Italian chef right now down here we can say Italian chef dot make special dish so you see up here we're saying normal chef make special dish down here we're saying Italian chef dot makes special dish so if I play this we'll see the normal chef makes barbecue ribs and the other chef makes eggplant parm but you'll notice that I can use all the same methods inside of this Italian chef as I did inside the normal chef so down here I can say like Italian chef made chicken and this Italian chefs gonna be able to make chicken right can make a delicious check-in and also we have that additional attribute which would be to make pasta so we can say Italian chef top make pasta and it just says the chef makes pasta ok so essentially what happened here was I created a normal chef and then I created another type of chef which is an Italian chef now let's say that we want to create one more type of chef so we're gonna create a Chinese chef so I'm gonna make another class so I make one more class and we're just gonna say Chinese chef so this person will be cooking Chinese food and we click finish so now we have our Chinese chef and let's say once again the Chinese chef is gonna be able to do everything that that normal chef can do so the Chinese chef can make chicken can make salad and can make a special dish I'm just gonna take all of those methods and I'm just gonna paste them in here and now once again let's say that the Chinese chef can also make an additional dish so the Chinese chef can make fried rice so we can say public void make fry and rice and this is just basically just gonna print out the chef makes fried rice so this Chinese chef in addition to being able to make chicken salad and special dish can also make fried rice and let's say that we want the special dish to instead of being barbecue ribs be orange chicken all right so we have a Chinese chef we have an Italian chef and we just have normal chef and over here in my app dot Java file again I can just create an object for our Chinese chef Chinese chef and again this chef can do everything that those other two chefs can do except it can't make eggplant parm like the Italian chef and it has a different special dish alright so essentially what I did was I created three classes and what I want you guys to notice about these classes is they have similar methods so this chef class has three methods this Italian chef has those same three methods and the Chinese chef has those same three methods but what I did was I basically just copy and pasted the methods from this normal chef into the Italian chef and into the Chinese chef and this is kind of a lot of work right if I wanted to create another chef for example like a French chef I would have to do the same exact thing I'd have to copy and paste all of those methods from the chef class into that new French chef class so this is a situation where we can use something called inheritance an inheritance is basically where a certain Java class can inherit other methods from another Java class so for example this Chinese chef class would be able to inherit this make chicken class this makes salad class and this makes special dish class from the chef class so it wouldn't actually have to include them in here so let me show you guys how this works inside this Chinese chef I can actually just get rid of make chicken make salad and make special discs I'm just gonna delete them okay so a hundred percent up front they're just gone what I can do over here is I can say public class so after I say the name of our class Chinese chef I can use a special reserved word in Java called extends I can say ext EMDs and now I can type in the name of the class that I want to extend so I can say chef and essentially what I'm doing here is I'm extending all of the functionality from the chef class so over here this chef Java file has a make chicken method a make salad method and a make special dish method and essentially by saying extends this Chinese chef class is actually going to have all of those functions included inside of it automatically so let me show you guys how this works I'm gonna come over here to this Chinese chef object and you'll notice that inside of the Chinese chef class I don't have a method method called make chicken or make salad but I can still access that method inside of this object so I can say Chinese chef make salad and when I print this out to the screen you'll see it says the chef makes a healthy salad so down here this is the Chinese chef is able to make a healthy salad so I'm able to use all of the functionality from that normal chef from that chef class inside of the Chinese chef class and that's basically just saving me a lot of time I don't have to like physically copy all of these methods and put them over into here I can just extend the functionality of the chef class and they're included automatically also then I can include custom methods so for example I can include this make fried rice method inside the Chinese chef even though it's not included in the chef class so I have all of the functionality of the chef class with the eyelid added functionality of being able to make fried rice but if you guys remember there is also the situation with the special dish so this chef over here is making a special dish making barbecue ribs but if you remember the Chinese chef had actually changed this so the Chinese chef special dish was orange chicken what you can do when you're extending a class is you can actually override some of the methods so what I could do is I could copy this make special dish method from inside of the Chef Java file and then over here in the Chinese chef Java file I can just make my own special dish method so over here I can say the chef makes orange chicken and now this makes special dish method inside of the Chinese chef class is gonna override the make special dish method that we got from the chef class so if I came over to my app of a file I could say Chinese chef make special dish and the Chinese chef is now going to be making orange chicken instead of those barbecue ribs so you can see here chef makes it on chicken so if I don't like one of the methods that I'm getting from the class that I'm extending from I can just override it like we did here in the Chinese chef class so I could do the same thing with this Italian chef class right I could basically just get rid of make chicken and make salad because those two methods are gonna be the same and you'll notice here I'm still overriding this makes special dish so the Italian chef is gonna make eggplant parm and the Italian chef is still able to make pasta all I have to do up here though is just say extend chef and this Italian chef is still gonna be able to make chicken and salad even though I'm not like specifying it inside of here so that's the power of extent that's why extends can be awesome any time that you're reusing or you're using the same methods in multiple classes like we did over here with these different chefs you wanna consider using extends you want to consider extending a class because honestly it's just gonna make your life a lot easier and it's gonna make it a lot easier to manage hey thanks for watching if you enjoyed the video please leave a like and subscribe to drop acad to be the first to know when we release new content also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below finally if you're enjoying chopped Academy and you want to help us grow head over to draft Kadim EECOM forward slash contribute and invest in our future
Info
Channel: Mike Dane
Views: 21,311
Rating: 4.974227 out of 5
Keywords: Programming
Id: ePFWoOnyYHc
Channel Id: undefined
Length: 12min 41sec (761 seconds)
Published: Sat Oct 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.