 Let's continue talking about inheritance. And let me pause just for a second here. I'm turning up the microphone there setting. Okay, so here's what we'd like to implement. In a store we have regular items. They have a name, a stock keeping unit, also called a SKU and a price. And here are our getters. There's only a setter for the price. Once you establish the name and the SKU for an item, they never change. We'll also have a method for purchase. You give it a quantity and it tells you how much it costs to purchase that amount of the item. And then a two string and an equals method. Sale item is going to inherit from item. And the extra thing that it allows is a discount. It has its own constructor and you can get and set the discount. It also has a purchase method, which is going to be different from this one because it takes the discount as part of its calculation. And it also has its own two string and equals method. So there's our plan. Now let's go here and take a look at the code. And here's the item.java. There's nothing particularly new here. When I have the setter for the price, by the way, I put an absolute value to make sure that we don't end up with a negative price. And I probably should do the absolute value of quantity here, but let's overlook that for now. And here's my two string. I can get the name, the SKU and the price. And for equals, the names have to be equal. The SKUs have to be equal and the prices have to be equal. I guess I should do some real quick test here. Let's do this as save as and let's call it quick item test.java. And that will be, so let's have an item and let's call it, oh, I don't know, water bottle. And that's going to be a new item. The string name is going to be water bottle. The SKU is going to be WB0101 and the price is going to be, let's say $5.98 and let's print out water bottle. And by the way, I guess we may as well compile this to make sure it compiles okay. Excellent. Let's go back here and let's compile again. Perfect, just want to make sure it worked. And let's compile this and run it. Okay, it looks like it's doing that properly. Let's just check here. Double cost equals purchase of five. And this is an out and dot print line of, let's do a printf. Purchasing 5% sign S costs $0.2F. And inside S I'm going to say waterbottle.getName and then waterbottle, well, it's called cost. I already have it there. And let's compile that. Well, that's exciting. Oh, yes, that's right. Because I have to say waterbottle.purchase five. There, that's much better. Okay, so wonderful. We know that our irregular items work. Now it's time to go to the sale item and now we're going to come into some problems. So my immediate instinct is to have the name skew, price, and discount and set the name to this dot name skew and price. And for getting the discount, I'll return this dot discount. Setting the discount, I make sure that it never goes above 100%. You can't have more than 100% discount. And then my purchase will be the price times the quantity times one minus the discount over 100. And to string, I'm going to give the name, the skew, and the price. And then also what the discount is as a percentage. To make it a percentage, by the way, I have to multiply by 100. And for equality, I forgot to do that one. I'll talk about it later. And all of a sudden I get all sorts of errors here. And the problem is, okay, first of all, let's see what line five is. Excuse me. I'm not sure why I'm getting this error message, but here's the real problem that we have. And that is I can't set the name. It has private access. Same here and same here. I can't say something like this dot set name of name. That won't work either. If I try and do that, it will say, I'm sorry. There is no such thing as set name. Remember we never provided one. So we're stuck. It would appear. Now the question is, how do we solve this problem? The way we solve this problem is with a special keyword called super. What we will do is we will call the super class constructor and give it the name, the skew, and the price. It's okay for us to do this because the constructor is public. So that means we should be able to do that just fine. If I now compile it, I still have all my other problems, but I've solved this one. When you use the super keyword, very important, the super keyword in a constructor must be the first item other than comments. So let's open up our notes here in a constructor. You can call the super class constructor with the super keyword. But it must be the first non comment line in the constructor. I don't know why I'm having trouble typing constructable date. Now here we have the same problem in line 21. We can't get to price and we can't get to quantity directly. Why? Because they're private inside of item. So we're going to sneak around that. We do have something public that will give us the price. It's this method. We do have something that will get the quantity. Did they even put that in there? Oh, no, there is no quantity. Excuse me. We can get the price. We can get the name and we can get the skew by calling the getter methods. Those are all public. So again, we have no direct access to these private attributes, but we do have access to them through their getters. And that's what we're going to have to do here inside of sale item. So instead of price, we're going to have to say this dot get price. It's inherited and it'll work. And we have one minus discount times 100. Let's compile that and see if that error message goes away. Perfect. We'll have to say get name. Get skew. And get price. We have no direct access to them, but we have access to their getters. And here I already did it in equals. So I say this dot get name is other get names. So for the, again, I need the getters for the name skew and price for the discount because that belongs to me. I can get to directly. No problem. So let's do something here and let's do something like, Oh, I don't know. Sale item. Call it, let's say headphones. And that's going to be a new sale item. With is going to be your piece headphones. And they're going to be. And let's have them priced at $34 and 20 cents. And let's have a 10% discount on them. Let's do a system out. Print line of headphones. And let's say the cost is going to be. Headphones. Purchase. Of. Let's do 10 of them. I know that that will work out. Okay. Because we can multiply 34.2 times 10 in our heads. And it's going to be headphones. Get name. And our cost. And let's just check to see if that works. Lovely. And is that about right? Well, let's go and fire up J shell here for a moment and find out. So $342 times. 0.9. There's $34 and 20, right? That's interesting. $341 and 66 cents. What am I doing wrong here? Okay. I've got to think about this. Okay. Something went wrong here in this cost. I have something wrong with my calculation. Because 10 of these should cost $34 and 20 cents. And there's no way I should get 33 and $41 and 66 off of this. So what am I doing wrong? Well, let's go back and look at our sale item. Java and look, let's look at this. Oh, there's the problem. I'm dividing by a hundred. I don't have to divide by 100. It already is. I don't have to divide by 100. I don't have to divide by 100. I don't have to divide by 100. It already is a decimal. Let's compile that again here. And let's come back here and let's rerun it. And there we go. Now we have the correct answer. Well, this is really interesting because I believe the book has it wrong. Because in the book, I have the code. As. Oh, no, I haven't written the code. Okay. All right. I'm happy now. I'm sorry. That just knocked me totally off track. Okay. Fine. We've got our inheritance. We have super. And that's just super. Now there is one thing that we can improve here. Let me close this off. By the way, I don't need it. And let's come back here. Let's take a look back here at sale item. You'll notice that this is the price times the quantity times the quantity times the quantity times the quantity times the quantity times the quantity times the quantity. And in this discount. And in regular item, when we had purchase, it was quantity times price, which is the same as price times quantity because. Commutativity. It turns out that we can save ourselves a little bit of code here. By saying. Super. Purchase. So there's another use of the super. Super to save ourselves a little bit of extra code. Is. Here. Inside of string. Format. Instead of having me to do this. Get named this. Get us skew this. Got. Get. This. Get price. Let's compile that. Let's check to make sure that still works. Compile this. And run it. And yep, we get our $307 and 80 cents. Another place that we can use super to save ourselves a little bit of extra code. Let's say this got get. This dot get price. Let's do super dot to string. Which will return us one string. And then the discount. Because super dot to string will give us. The name the skew and the price. Which is what we wanted. Let's compile that. And as you see. Everything still works great. Let me see if there's another place that I can use super. Oh, yes, I can. All of this code. Is exactly the same. As. This code here. That means I can say. Super dot equals. Of other. This dot discount. I'm pretty sure this will work. Yep. And let's just check to make sure that that is indeed the case. And yep, we get true. So that's another use of super. You can also call methods. From the super class. By using the super dot. Method name. There's no law that says you have to, but it will also reduce your code. And then that also means if you ever change the way that item works. Sale item will pick it up automatically for free. Such a deal. Okay, now the next thing that I want to move on to. Is polymorphism and dynamic binding. And this is really quite interesting. So let's go here. I hope I have this. Polymorphism dot Java. Yes, wonderful. And since I copied and pasted this, I guess I should put some comments here at the beginning. So. So that you can assign a subclass object to a. Super class variable. There's nothing here that's surprising is going to surprise anybody. I'm creating an item object. I'm assigning it to an item variable. No big deal there. Here's something that's really weird though. I have a sale item. Instead of assigning it to a sale item variable. I'm assigning it to an item variable. Is that actually legal? The answer is, yeah, it's perfectly legal. And in fact, let's run this. Now here's, here's something interesting. What do you think is going to happen? Inside of item one, there's an item object. Well, first of all, let me ask, why is this even legal? Why can I even do this? The reason I can do it is because of something called polymorphism. If that polymorphism means you can assign a subclass object. To a superclass variable. If we want an analogy, and it's a pretty crappy analogy, but I'll go with it. Remember the bicycle and electric bicycles we had before. If you have a store that sells bicycles, you can have them sell both regular bicycles. And electric bicycles. Because an electric bicycle is a bicycle. It inherits it with extra features. However, it doesn't work the opposite direction. If you have a store that sells only electric bicycles. You can't sell a regular bicycle there. So essentially the superclass store. Can have subclass bicycles in it, subclass items in it. Whereas here. A subclass store, the electric bicycle store. Can't have superclass items in it. And in fact, that's why this is wrong. I can't say. I'm going to create a new item and assign it to a sale item variable. All sale items are items with a discount. But not all items in the world are on sale. And in fact, if I do this, it'll say incompatible types. Item cannot be converted to sale item. Now comes the next question. Let's recompile this by the way to get rid of that error. As far as the compiler is concerned, both item one and item two are item variables. And I call the two string method for item one is going to call two string from the item class. There's no question about that. When I call two string on item two, which one is it going to call? Is it going to call the two string method from item? Or is it going to call the two string method from sale item? We'll find out when we run it. If we see the discount in our output, then it's calling the sale item to string. If we don't, it's calling the items to string method. To run it. And we see the discount. And this is what's called dynamic binding. So dynamic binding. Means that at runtime. The JVM will look at the actual object. To determine its class. And then call the method. From the objects. True class. No matter what we told the compiler. So there's two phases to this. And this is really important. When I have this. As far as the compiler is concerned. Item two is an item object. When we run the program and we have to call this two string method, the Java virtual machine will look at what is inside this object. And find out, oh, this variable really contains a sale item object. And therefore I will call the two string method from the sale item object. From the sale item class, excuse me, which will show me my percent sign. Great. This is actually what we want. It's very convenient indeed. However, this leads us to one other problem that we might have. Let's go and look at this. In this one, I have an array of items. Because of polymorphism. My array can contain regular items. Like the rye bread and the organic salsa. And it can also contain sale items like tomato soup, canned lime of beans and frozen pizza. So I'm going to go to eat through each item in the foods array and I'm going to print out its name. I'm going to get rid of this stuff here. Because I want, I want to build this up step by step. There's not going to be any problem here. Why? Because. That name is inherited in the item class and sale item. And therefore I will always be able to print out the name and the price. Let's compile those and run them. Here's something I want to do. I would like to print out the amount that you get discounted for the sale items. So how much are you saving is the question. The amount that you save on a sale item is going to be. The food dot get price. Times the discount, which is food dot get discount. Let's compile that. And now all of a sudden I ran into a problem. Get discount. Only belongs to sale item. It doesn't belong to item. And you'll notice here I've said that foods are an array of items. So now what am I going to do? I want the discount. First of all, since a regular item, right, bread doesn't have a discount. So it doesn't make any sense to try and get the discount for the right bread or the organic salsa here. I have to say, well, if I have a sale item, then I want to get the discount. To do that, I use the instance of operator. So I'll say if my food is an instance of sale item. Then I want to get the discount. Because only my sale items have them. So let's go back here. And on the notes, the instance of operator sells you at runtime. If a variable contains an object of a given class. We're halfway there. The problem is, again, we have the compiler versus runtime. So at runtime, we're going to find out that the tomato soup is a sale item. We're going to find out that the cab lima beans are a sale item. And so is the frozen pizza. But as far as the compiler is concerned, no, they're still regular items and they don't have a discount. And so the compiler is still going to complain when we go to here to try and get discount. And you'll see it says, no, you can't do it. Okay, so now we've established that. Yes, we have a sale item. So here's what we're going to do here. We're going to say sale item sale food is going to be sale. We are going to use a cast. We're going to force the compiler. To treat this item. As if it were a sale item. It's okay to do this is safe to do this because we know that our food variable contains a sale item. So forcing the compiler to treat it that way is perfectly okay. Now we can get the price of the sale food and we can get its discount because it's a sale item as far as the compiler is concerned. Anyway, let's do a blank line for readability. And now for the sale items for the tomato soup, the lime of beans and the frozen pizza, it tells us how much we save. For the regular items, it doesn't do that. It's a two part process. So if you have a polymorphic array, and you want to call a method that belongs only to the subclass, you have to first check to see that the variable really does contain a subclass, the subclass variable. And then you have to cast it to make it work. So essentially would say if variable is an instance of subclass. And I'm going to say you can say subclass variable is, and then I can say sub bar dot, or we can reduce it to a single line. We can say if the variable is an instance of the subclass, then we can say subclass variable, and we need an extra set of parentheses to make the priority of operations work right. And when I say cast it to make it work, that's too vague cast it to convince the compiler to treat the variable as a subclass variable. So let's go back here to the polymorphic array and let's save this again under a different name, let's call it polyarray to, and what I'll do here is I'll do it as a single line. First of all, I can say food dot get price by the way. Why because get price is inherited and there's no problem there. Times sale item food dot get discount. I have to do the cast to convince the compiler. Yeah, yeah, I know this food here is part of the foods array and it's a regular old item, but just for now, treat it as though it were a sale item. Then at runtime, we're going to check, do we really have a sale item here and if we do, then this cast will work properly and we'll call the get discount method from the subclass. And again it works as before. It's two step process. We have to do the conversion as a cast for the compiler. And then at runtime, polymorphism will take over and dynamic binding will find the correct method to run. That's pretty much it for chapter 14. But this is a very important thing. Let me take a look at some of these exercises here. Okay, I've got a damn polymorphism with those damned bicycles again. Oh, there's only two and one of that happens to be the assignment. Well, I may have to come up with another example for I may do another mini lecture tomorrow and if I can come up with another example of polymorphism and dynamic binding that goes through the same sort of thing I will do that. So that's it for tonight. See y'all online.