Java Inheritance and Constructors

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody this is a Brian Fleischman again I'm back here talking about inheritance today you saw the title of the video we're going to get into the mechanics of constructors and how it's kind of weird when you're sub classing other classes and how the constructor thing works a lot of people are seeing errors a lot of people are kind of asking me questions about how constructors work okay so let's go ahead and do this okay I've gonna start a new class in here I'm going to close my package Explorer okay so it's not in the way I'm just going to make a class called how about mammal okay and that's all it's going to it's all it's going to be my source folder I need to actually pick one so let's pick sorry about this as the stuff I probably could have done behind the scenes how about let's pick that one okay so this is stuff you probably don't have to do so here's my class mammal let's make another one so what's something that is a mammal how about primate would be now when we have superclass here we can actually choose the superclass but we're not going to do that right now I'm gonna show you a different way if you ever want to extend the class you can always kind of navigate through here and pick the type then we're gonna have to like you know dig through a folder structure to find it so I don't think this is the best way to do it so let's just let superclass be object for now and then we'll notice that we have mammal and we have primate since every primate is a mammal we can say extends mammal I'm betting that you know a little bit about inheritance so that this is not surprising if you need some more brush up on in general inheritance stuff and what this word means we'll have to talk about that in a different video so I'm going to make up some very very bogus and probably unnecessary field variables for mammal so we might have for example every mammal has let's say a weight and then maybe how about a string of hair color I don't know okay so every mammal has hair okay so we might as well have hair color for the mammal okay so being that primate extends mammal essentially it gets those fields as well so every primates going to have those fields we just don't have to write them again okay so let's write a constructor for actually before we go there let's make a new class it's gonna be a testing class okay how about a test test how about that so this one's going to have our main method if you have eclipse you can have this I don't know if you guys need this check this box that adds a main method for you so let's see can we instantiate a mammal right now let's just I'm just curious can we instantiate a mammal let's try it so mammal my mammal I don't know remember you get to choose your variable names in equals new mammal and I haven't built any constructors so I guess this would work and I guess it's not that a it's not an error just saying you never used it other than creating it so that's not a problem can we make a primate my primate equals new primate now the reason I'm doing this is because I need you guys to remember that every time that you make an object class like this even if it's empty Java will give it a constructor for free okay a constructor that it gives you is one that looks like this okay a constructor always starts with I guess public and then the name of the class and it's parameterless and the body is totally empty so essentially this is what it gives you for free so I've literally actually added no functionality to the this class interaction here it's going to be the same note nothing changed so if I delete this and Java sees that you have not provided a constructor for your classes it'll go all I feel bad for you if you don't know how to make a constructor so I'll make you one behind the scenes do we see that written on the screen no we don't but it's okay Java takes care of it for us so so far we have these now here's where people start running into problems right so for example with this mammal right when people want to make a constructor for this public mammal and we have we're gonna pass in some parameters they're gonna pass in a weight and they're also going to pass in a hair color okay so if you need so I'm gonna assume that you know stuff about objects and constructors a little bit and that the new information is this this interaction between super classes and subclasses okay so I'm not going to go into all the mechanics of constructors you guys should know what this dot weight equals weight you're just not hair color people's hair color you've probably done a zillion of these things before alright so then I save and then I start getting problems did you see that well first of all this is not a surprise to us right if you know anything about constructors Java saw now that we wrote a constructor for mamillus so it doesn't feel bad for us anymore and it doesn't give us that pity constructor that default constructor for free it just takes it away so goes oh they know how to write a constructor so this is the only one that exists right now so when we asked the test class to build the new mammal using this parameter list constructor no-go it doesn't exist anymore okay so I guess we could fix this by adding some parameters here how about this mammal weighs like 2,000 pounds and has a hair color of black okay maybe a gorilla or something I don't know so there we go that works now do we have a primate though let's see this code is not giving us an error but primate is giving us an error okay what let's see this is the kind of stuff I see in class and I'd like to explain it to everybody at the same time right so they hover over this and it makes them want to quit because it says implicit super constructor mammal is undefined for default constructor and when you read that it makes a lot of sense to nerds but maybe to us you know peons it doesn't make a whole lot of sense but let me explain it to you what it's trying to say okay so let's again write this it's this constructor that it gave us for free public primate okay so remember this is the one that it gives us for free okay so the weird thing about when you have a subclass right a primate is a mammal which means that every track every time you try to instantiate a primate you actually also need to be creating a mammal now think about what that means in the real world that makes sense right every time a primate is born I guess a mammal is born - right you can't actually have a primate without having a mammal so the code actually needs to simulate that right so by making a primate we actually need to make a mammal now what's the only way we have to make a mammal oh we have to make it using this constructor okay so we actually need to as our first line of our primate constructor because we made a primate we need to make a mammal so let's let's access the superclass's constructor and here's how you do it right you actually need to use the word super that's you see it light up it's a keyword and then we need to pass in some stuff right so I guess you know we don't get any you know the way we've written this constructor we don't have any parameters passed in so I guess just for this example we're gonna have the uncomfortable task of just picking some random numbers for the weight I don't know 100 pounds and a hair color which is brown eyes close right now to have control over these we're going to need to adjust the the parameter list of primate now do you notice that it's kind of happier now I saved and now it'll get this right so now it's happy so a primate is born and because a primate is born a a mammal must be born well it's the only way to create a mammal we have to use the constructor which is takes a weight and a hair color now remember just what super means is you can kind of exceed this way right what is the superclass - primate well let's look what it's done this mammal okay so the super which is the mammal constructor can you see it if you have over it okay so we need to use that now so let's go back a little bit okay so let's go back and write in the code I'm gonna back up and I'm actually delete this case hopefully got some understanding out of that and I'm gonna delete this one okay I'm gonna save and I'm gonna go delete this stuff and not the whole thing but you know just kind of just so I don't have any errors anymore and I've got problems okay now we're good okay so remember when we had it just like this and we didn't even have these right so what really is going on here behind the scenes is that we have the freebie constructor public primate remember it's the freebie constructor that has nothing in it and mammal also has one right so I guess public mammal so this is all get written behind the scenes I just want you to know that what I've just done added absolutely no new functionality to these classes okay but didn't you say just a second ago that every time you make a primate you need to create a mammal I don't see any creation of a mammal in there that's a really good observation but guess what Java does right it thinks to itself well I know we need to make a mammal I wonder if there is a default constructor available to me a su default super constructor available to me let's look there is right this is all behind the scenes but I'm writing it out for you so I guess what really it's doing is this it's saying super right and now what what parameter list does this mammal class have it's an empty one right so this is really what's going on so if we could see behind the scenes okay if we could see behind the scenes of these classes before we wrote these in this is what would be there this is what Java does for us it makes a default mammal constructor think about that it makes a print of the freebie right it makes a freebie primate constructor and because it knows it must extend mammal it tries to see if there's a freebie super constructor and it turns out that there is so that's why that worked that first time you remember when we had just totally empty classes and I created a mammal you know malim him oh my my mammal equals new male all right this constructor worked because I guess they gave Java had this one written behind the scenes for us for free with the primate one we're almost there but you guys are already getting this but I'm just kind of going over it one more time primate stuff equals new primate before we had written the constructor Java had given us the primate freebie constructor for free and this is actually what the freebie constructor of a subclass looks like it has no parameter and it tries to reference a super constructor which is also a default right so let's just see what kind of things break this this is gonna be I don't want this recording to go too long but I wonder like let's say if I kind of take this out this is what's written behind the scenes so really I haven't taken away any functionality so if are we good I wonder if I start adding parameter lists to this okay say I just have a name Wade okay just I'm going to do one perfect one and wait and I do something like this that weight equals weight and I'm gonna save well just as we thought might happen there's a problem now now why do you think so what's written here behind the scenes do you remember it's this now this can you this is what reveals the problem there's no longer a default super constructor right because this no longer has a default super constructor we have to actually you know probably use this constructor right the one that takes a wait right so we have to either you know do this pass in and in or I suppose what we could do is maybe explicitly write the default constructor remember if you write your own constructor Java takes that default constructor away but maybe if we want to still have that functionality you can say public mammal and write your own write your own default constructor so Java now just because it didn't write it for you doesn't mean you can't have one you just have to explicitly say that and do you see how this is now happy so if I you know delete this stuff it's still it's fine right we have no problems right because you know let's just do this more so with low let's look at it Java gives you this for free does the super constructor have an empty one does the superclass have an empty constructor yes because we actually expelled it out okay so maybe a little bit a little bit crazy at first but I swear if I can understand it I know you guys can too that's kind of the mechanics of subclass and superclass constructors I hope that cleared things up and please just leave me a comment or a message if you want me to go into anything any further if you have any specific questions about constructing objects of super class and subclass relationship I hope you enjoyed this this video please leave me a message a comment I love when you guys do I'll talk to you soon happy coding
Info
Channel: Brian Fleischman
Views: 5,607
Rating: 4.8717947 out of 5
Keywords: Java, AP Computer Science, Inheritance
Id: kGLLtMIamuQ
Channel Id: undefined
Length: 12min 57sec (777 seconds)
Published: Fri Mar 24 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.