all right ladies and gentlemen welcome to my video in this video i'm going to be going over object oriented programming in kotlin in the previous content tutorial i went over the basics of kotlin and i'll leave a link in the description uh to my blog post for that as well as my playlist to that but in this video i'm going to be going over the concepts of object oriented programming in kotlin object oriented programming is a concept that uses classes and objects to store and manipulate data this data is stored in the form of fields such as attributes or properties and methods methods are just functions within the class so we're going to dive in a little bit deeper upon that and the first thing i'm going to talk about is classes all right so a class is a blueprint to create an object so if you guys can see from my very amazing drawing you can see that we have a house all right we can have different properties for a house so that's what the blueprint should describe it should describe you know the amount of rooms the amount of bathrooms the color of the house these are properties of a house these are the things that a blueprint should have all right and then we have actions all right this is going to be the methods of the object the things that uh the house can do so we can open the door we can close the window we can paint a new color these are the methods of an option object this is going to be the functions that you have within a class so i hope that helps clear it up a little bit a class is a blueprint of an object and a class contain properties about the object so in this case the object is the house you guys can see the windows you guys can see the doors and on a deeper level these properties describe the amount of rooms the amount of bathrooms the color of the house and it can do actions or a object contain methods or class contain functions that is called the method when you create the object which in this case is a house you can use it to open up the door you can use it to close the window and you can even paint a new color that's that's cool so now we're going to see in kotlin how to create this blueprint so if we switch over see how to create this class with this blueprint in kotlin so as you guys can see i have this main function and really this is already going to be in the application start um file i'm going to keep the class in the same file but later on we're going to write its own file for it but in this case i'm just going to put it right below the main function and i'm going to put class and in this case i want to create a house and then you open up some curly braces and within these curly braces this is where we're going to start writing some properties to describe the house or create some functions to describe an action that the house can do but another cool thing about colin is that if you have no properties that you want to use to describe the class or the object you can just omit the curly braces and this will just signify that it's going to be an empty class and this is perfectly fine this is still a class but in our case we want to actually write some properties about the house so we're going to do that before we start creating properties we should first think about how we're going to pass data to those properties if you think about a blueprint a blueprint is not usually used just once usually it's used multiple times so that you don't have to keep creating multiple blueprints that saves time but there has to be a way to make the house a little bit different so maybe we might change the amount of rooms maybe we might change the amount of um bathrooms maybe we might change the color just to make the house a little bit different but how are we going to pass this different data to the blueprint or to the class in our case we're going to use a constructor constructors are used to pass in data when an object is created in our case a house so when a house is created this is how we're going to pass data into it in kotlin we have different types of constructors we have the primary constructor and we have the secondary constructor you could only have one primary constructor so i like to call this the base constructor but you can only have one primary constructor but you can have many secondary constructors and in kotlin we still have constructor overloading if you guys program before constructive overloading is when you're writing multiple constructors but the parameters are different they can't be the same because that'd be counter-intuitive if each constructor have the same parameters we can use different amounts of constructors to specify what parameters that you expect so maybe we just want the user to pass in rooms we can have constructor for that maybe you want the user to pass in a room and the color of the house you can have constructed for that so constructive overloading allows you to have multiple constructors to allow for different parameters to pass in data so the primary constructor let's start that off to create a primary constructor all you got to do is place a parenthesis right after the name of the class boom and we start we can start writing parameters that we want to pass into this house class or right into the blueprint you also have the option to write out the actual word constructor and then place the parentheses right after that but that's optional and honestly i don't like doing more work than i have to sometimes in this case if it's not necessary i can admit it before we talked about rooms we talked about bathrooms and we talked about color for the house so let's add that as the parameters so let's add rooms i'm gonna make that an int let's add bathrooms i'm gonna make that a double because sometimes you know some houses you know they can have 2.5 bathrooms because you know one of the bathrooms will come with a tub and then we'll add color and this is going to be a string and boom so now when we create a house we expect the user to pass in rooms the amount of rooms the amount of bathrooms and the color so for a site like zillow on a very basic scale this is perfect and for the primary constructor we can even have default values so if you think the user is going to put something sketchy or they're not going to play anything at all we can you know put in some default values and you know if they don't put anything in then we'll just use these values and then first string we can place that we put empty string but in this case i don't want to do that you know i want to use it i want to force to use it to place in data when they create this house the next type of constrictor we have is the secondary constructor now to create a secondary constructor you actually do have to use the constructor keyword and in this case it's not optional but it's in a different place now it's within the body of the house so you just type in constructor and then you open up parentheses and it actually comes with a body so if you want to run some code after the user passes in data you could and just like the primary constructor before you just place data within the parentheses so in this case i could also put rooms and i could put int i could put bathrooms and i could put double for that and then i can put color and i can make that a string so yeah and then i can print ln and then i can print out some where i can print out the colors i can just literally just put color and color and boom so when a person creates a house and we're going to show that later on when a president creates a house and they pass in the room bathrooms and color this will run right after so take the color that is being passed in and it will print it out so that is the secondary constructor so we're not just restricted to just using you know the secondary constructor individually and the primary constructor individually we can use both of them at the same time so let's see that in action so currently we just have the secondary constructor right now there is no parenthesis after the class name so that means there is no primary constructor but if we add a parenthesis right after we're including the primary constructor but as you guys can see a red wiggly line starts to appear it says the primary constructor call is expected so when we add a primary constructor and we have a secondary constructor it still expects us to call the primary constructor first it should be the first constructor called when the object is made to include the primary constructor we just add in a colon right after the parenthesis in secondary constructor and we use the keyword this oh that's how you spell this this and this pretty much means that we're going to make a call to this primary constructor and you have to add a parenthesis right after this and this will make a call to the primary constructor right now we have no parameters in the primary constructor so we don't have to include anything within it but if we were to have something like color so let's put color string now it expects you to pass in a parameter to this primary constructor even when calling the secondary constructor we still expect uh to pass the parameter to the primary constructor so to do that uh we're taking color in the secondary constructor we could just take that color variable which is right here and we can just pass it to the primary constructor and that should cause a flickering line to go away so that's how you make a call to the primary restrictor even when using the secondary constructor and let's do a little example because we're always talking about passing in parameters or passing in data but we don't even know how to do it so just to show you guys how to pass in uh values let me just put a tab let's call val and we're going to call this house we're going to make this equal to house right we're going to open up the parentheses and now we're making a call to constructor c has given us two options we can either make a call to the primary constructor or we can make a call to the secondary constructor in this case i want to make a call to the secondary constructor so i'm going to pass in the amount of room so i want a four room house with five and a half bathrooms and i want the house to be blue you know very mellow and if i run this with a little bit of time you guys will see boom color is blue so pretty much we're creating a house we're using this parenthesis to call the constructor we're choosing the secondary constructor based on the parameters that we passed in it's saying that we have a ins for rooms a double for bathrooms and we have a color for string and first when we're even though we're passing this data to the secondary constructor it's still taking the color that the person passed in which is blue and it's making that call to the primary constructor first and then it's going to run this uh println color and then the color that the person and put it into the constructor which is blue so the primary constructor comes first and the secondary constructor comes last and there's something in between that we're about to talk about which is called the init block before i show you guys the init block i want to show you guys that you don't necessarily have to call the secondary constructor you can also just go by the parameter of the primary constructor which in this case is just accepting a string for the color and if we put red and we run that we can see that the secondary constructor doesn't even get called there is no printout it doesn't even touch this it just gets that value and that's it the primary selector just retrieves the value but what if we want to hold on to that value how do we do that we're gonna need class properties for that so two use class properties or to make class properties we just create variables for it so in this case we're going to have a variable that holds color and we need a place or actually just make this a string right and now we just need a place to initialize this data we need to get the values or the data that the person is putting in and find a way to pass that value to the class properties so it says property must be initialized or be extract in this case we want to initialize it so we're going to use the thing called the init block all right this is where we're going to initialize the class properties and it's just using the init keyword and we open up a body using the curly braces and then now we can pass in data to the class properties that we have above or clash property because we're just using once right now and within this emit block we can run any code we can print we can you know store values we can make another variable but for this case i just want to take the value that the user is passing in and i want to pass it to the to the class property so the first thing we're going to need is this because the fact that i made this um property called color it could probably be confusing for you know people that's going to view my code so i'm going to use this this keyword to signify that i'm taking the property of this class uh which in this case is going to be color and i'm going to make it equal to color and you guys can see the twiggy lines go away or the red one goes away but yeah i'm going to take this property of the clash call color and i'm going to make it equal to color which is going to be the color that the user passes in through the constructor and there's an order to this thing right so the order is the primary constructor is called then the init block is called and then the secondary constructor is called last so i like to think of the init block as the body for the primary constructor because as you guys can see the constructor has its own body right but the primary constructor doesn't have his own body we have to use the init block to initialize the data so or to run some code if we wanted to so that's what the init block is good for you can run some code or you can initialize the data if needed so just to show you guys that the nip block is actually receiving the value that we're expecting from the user we'll just use the print and ln in the init block and this is ran after the primary constructor like i said so we're just gonna pass in this dark color and we're gonna run this and we should expect to see red all right cool stuff so now we're not just restricted to just using one init block we can use multiple edit blocks so if i just copy and i paste this right under and i'm going to add in another class property so i'm going to call this one rooms and i'm going to make that into an int and we're good to go it's going to have the same problem we're going to say that this property must be initialized so i'm going to just use this.rooms and i'm going to make that equal to rooms and i have to add that to the primary constructor and i'm going to print that out as these are rooms and let me just add this to the primary constructor so then it's going to be rooms and so now you guys can see that the parameters change in the primary constructor so i have to add this i have to pass it to the primary constrictor in the secondary constructor so we're going to take that rooms and pass it to the primary constructor and now it's letting you know you've got to choose between using two parameters or using three parameters i still want to use the primary constructor so i'm going to use the two parameters i'm just going to pass in two as the number of rooms that i want and if i run this i should see red and i should see two which is exactly what i see and which one runs first just it's based on what order you actually put the nip block in so it's going to run the color first and then it's going to run the rooms after but if i if i just take this and put it right above this color a knit block and i paste it there and i run this boom two and red so this shows you know how powerful the knit back can be uh and if you want to keep things short maybe you want to keep it you know in a small context maybe you'll use the multiple init blocks but you know it's it's it's possible if you need to so before i talked a little bit about constructor overloading so let me just copy this so you guys can see exactly what it is i'm just copy this and then i'll paste it right under it right now it's going to show errors and conflicting overloads because we have the same parameters and that's redundant but all you got to do is either added a parameter or take out a parameter so in this case i want to keep coloring runes because i know we have to pass that to the primary constructor but i want to add in another parameter called the hoa fee this is the house owner association fee you know so it may be zero or it may cost something based on the uh place of you know of the house so if it's in the community or something you might have to pay a fee and we're going to make this a double because we're talking money and the primary constructor you know maybe we don't need that maybe we don't have to pass that value but if the person does pass in the value then we're talking about this constructor so now we have multiple constructors we have one that takes in rooms you know bathrooms color then we have one that takes in you know the same thing as above which is rooms bathrooms and color but it also takes in the h-o-a fee but the primary constructor only cares about the colors and rooms so we still have to pass you know color rooms to the primary constructor because no matter what constructor we use even if we're overloading constructors we still have to make a call to the primary constructor using the parameters that they expect but now let's pass in the amount of room so i want to make it four rooms this time and i want to make it 5.5 bathrooms the colors still gonna be red but i want the feed to be five hundred dollars and i'm gonna put it point zero because it's a double and if we run this let's print out the hoa fee so you guys can see that it's actually getting there and if we run this we should still expect the init block to run because it runs right after primary constructor and then the secondary constructor run so let's see that in action actually let me change this to aj and let's run that boom so we see the amount of rooms we see the color of the house and now we see the hoa fee we don't see this color because of the fact that we're not calling the secondary constructor but we are still getting to this secondary or we are still getting to this init block or these two init blocks because you know we call that no matter what right after primary constructor and then we're getting to this last constructor because of the fact that we use different parameters so we don't even touch this constructor that uses three parameters we use the constructor that used four parameters because we passed in four values so it makes a lot of sense and we're still able to store that value because we're passing the color and room to the primary constructor so then it's just taking those values that you pass to it and it's using these knit blocks to initialize it so that is the power of the blueprint that is the power of creating an object and that is the power of constructor overloading all right so now let's start defining behavior for this house so to define a behavior we can use a method a method is simply a function that is associated to a class so all methods are functions but all functions are not methods so let's take a look we're going to create a prompt because maybe if we're a real estate agent we want to be able to give a small description to a user about how the house look like so we're just going to go by what we see in the primary constructor which is the color in rooms we want to let the user know you know what this house looked like in a nice neat prompt so to create a method which is just a function associated to a class we simply just use the fun which is you know to create a function and then we're going to make a simple prompt and then we're going to open that up and we're just going to use the properties that are already defined in the class because we know we're going to need we're going to need to pass in color in rooms when we create an instance of our house we know that's available to be used so we can just have a print and we'll just make a message so we'll just say the color of the house i can't spell today is and then we'll just pass in the color variable using the dollar and then put in color and the amount of rooms and then we're just going to put like a little and then we're going to put this let's put a little coal in there so now we have a simple prompt defined in our class now to use that behavior we have an instance of a house stored here in this variable called house so to make a call we would just take that instance of the class called house we use a dot and you guys can see what what are available from this instance of this class or from this object so now we can see the simple prompt we want to select that so we just use a dots we get that simple prompt method and we pass in the open parenthesis usually if you want to pass in parameters you could but since we're not taking any parameters we just leave those blank now if we run it and as you guys can see the room is going to be for the bathrooms is 5.5 the color is red and the house association fee is 500 if we run this you guys will see that boom the color of the house is red and the amount of rooms is 4. so it is unique to the object that we created so you guys can see that we're creating a prompt based on the color and rooms that we passed in in this constructor and we're using those values to make a simple prompt and if you want to make things more um you know different or specific you can also you know start adding in parameters or do different things like that so if we want to do that we can just do another one and we can't overload that but we could create another name i'm going to call this medium prompt you know before it was a simple prompt now it's a medium prompt and we could pass in some values so let's see um and we can pass in maybe a boolean so um we're going to have a variable called includes pool we're going to make this a boolean and we're going to do this and then we're gonna pass in includes pull now we just use that same object and then call medium prompt and now it's expecting a parameter we're gonna just pass in false because this house doesn't include a pool and if we run that and let me um to make this a little bit cleaner and then we comment these out and these i have a lot of prints so the only we should only be expecting two and let me use ellen here and ln here so they'll be on separate lines and run that so boom the color of the house red and the amount of rooms is four and the color of the house again using the same prompt but we now had a additional parameter that you know acts whether it includes a pool and above here we said that it's false by passing in the false value and parameter and boom and it includes pool and that is false it does not include a pool so you can have to go to the lake so that is how you create methods in kotlin for a object it's simply a method is simply a function within a class that is you know it's associated with a class so in this case these problems are associated with the house class so these are the methods hope you guys enjoyed that and hope you guys understand what a method is all right so just for a little bit let's dive in a little bit deeper into kotlin so if you guys ever had any experience with you know a programming language like java or any other object-oriented programming language you probably know that when you create a property in a class we would have to create a getter or setter to retrieve a value from an object or to set a value to an object or to provide a new value to an object in kotlin this is auto-generated and if you guys ever had any programming experience before that you don't know what i just meant let me show you guys what i mean so we have this instance of a house right and we set the value to green for the color and we set the value for rooms to five so now if we want to get that value all we got to do is use the variable that's holding that instance of that class or the object and then we just do dot color to get that value all right and if we run this we should expect to see green now if we want to set that value which means to assign it a value we can do that too i can change this to red if i want to all right and let me print that out just to show you guys so if i print out house dot color and i run this again boom green and red and that's because this is also a var so it's mutable that means i can change it but that's how we set the value this is a setter and when we're trying to print it out when we're using the dot color we're not assigning a value we're just getting the value so these are setters and getters and under the hood kotlin is generating that for us in previous language and other languages we would have to define functions to set a value to assign a value to a variable or property we will have to have another function to get that value from the object but in this case we're able to just set and get because kotlin does that under the hood for us it's auto generated now the cool thing about colin is that it does give us the option to change that if need be if we want access to those functions we do have access to it now let me show you guys how to do that as well so if i go into this house class right and i put get well you guys already see intellij has already given me clues but if i put gets and open close parenthesis and i make that equal to you know let's make this equal to black right this is pretty much just saying that every single time i try to retrieve you know this property or try to get the value of this property i'm gonna make this equal to black now if i run this no matter how many times you know i set it to different things so i see here i set it to green and here i said it's red when i run this what you guys think is going to look like it's going to be black twice because whenever i try to get the value right i'm assigning it to black i'm i'm making it black so no matter how many times i try to set it it's going to be black now by default it's usually just going to be filled and this is you know the variable name that they use but it's filled that's why you know field is not assigned anywhere but it's it still works because this is what's holding the value that i'm setting um and whenever i use that get now if i run it again it worked like it did before field is pretty much just holding the values that i said which is green and red now i also have the option to adjust the set function so we just have to get function whenever we try to retrieve a value this is what's being returned this field is being returned now where is that field being where's that field variable being changed where's that variable where's that um where is this data coming from so if we do a set and as you guys see again you know until it just gives me clues and we have this parameter this is value this is the value that we're passing in so in this case it'll be red and green this doesn't have to be the word value it can be whatever we choose we're gonna use use data as well um and then we can open up some brackets and then here is where the logic is going to be all right so here we use we use the variable name field just like the field here this is why it's it knows what field is because by default field is is used in the set function and we're going to make that field equal to data so this is how it knows like all right we're passing in a value that we want to be set so we're making this equal to red this red goes to here it's now red data holds that red value and we're setting the field equal to red so this data is the same thing as red because it's being passed in and now field is equal to red so then whenever we try to get something filled the current value of field is red so it's going to get that you know red value but we can also do the same thing here what if we don't pass in data that the that the user is trying to put in we can also still we can make this you know purple and whenever we do the set we do have to initialize the value here so we can just make this an empty string um but then that will all be changed when you know a set is done and when it gets done so if we run this we should expect to see purple because look whenever i try to do a sets right no matter what the person passes in i'm just going to put purple and now that this field value is equal to purple whenever i try to do a gets it's just going to take this field that i'm assigning the value to which is purple so it's always going to be purple so in a cool sense though we could add some logic to it so here let's do let's do a set right and we can do that and this also has to be initialized when we try to adjust this this set we have to initialize it so we're just going to make this equal to zero and what if we you know have you know a person pass in a value for rooms and for some reason we want to double it for some reason um we can just do field and we have access to the value that the user passed in right so we can just do value and if we want to do some other stuff that we could so if i want to times it by 2 i could do that and if i want to print that out so you guys can see now for some reason i have doubled the rooms and i'm gonna just pass in a string template real quick that holds the house dot rooms so look you guys see that look i'm passing in green and i'm passing in the room as fives i never set the value of five uh uh i never set the the room amounts anywhere else i know it's gonna be five but then when i get here i'm setting that value that that i'm passing in which is five and i'm multiplying it by two so what should i expect to see some quick maps if i run this i should expect to see ten so i doubled the rooms so these are the things that you have access to do yes carly handles some things you know in the background but you have access to manipulate the logic if need be and colin gives you the freedom to do that so that's the cool thing about kotlin you have setters and getters and we can just change this back into fields and it should work as it did before oh what is gonna oh my bad see that's that's where i messed up is data i make mistakes too so boom green and red so phil didn't have a value so it was empty but now i know that the data that the user is passing is is going to be what i assigned the field and that field is going to go to whatever that user tried to retrieve so cool stuff if you guys ever want to change the logic of the get or set you can do that just by putting it under the property and assigning and using these um variables so all right so now that we got some knowledge in our back pocket about op we start talking about an important concept in op which is inheritance we're going to start building relationships inheritance just like the name implies allows us to inherit from a class a base class a parent class this is the class that subclasses will retrieve properties from so just like in real life how you have a parent and you know you have siblings and sometimes that sibling may get that that compliment like man you look just like your parent because there are certain properties that your sibling or even you have that look just like your parent you're not the same person but you have properties that you can grab from your parents like i said your nose your eyes you know the way you speak so the same thing implies an op you may have a parent class at base class that is similar to the subclasses like they're they're the template and you have subclasses that branch off from that so that's the important inheritance when a project gets big and you know that there's going to be classes that are very similar you might want to have a template that those subclasses can retrieve from so in our project we've been using the house pretty much for every example in relation to you know a class and objects so what is a house in the grand scheme of things a house is a building and there are various different types of buildings and it's just not a house it can be an apartment a skyscraper and what makes these things different is many things like a house can be only one resident well apartment can be a list of residents or a list of tenants and a skyscraper can include a list of corporations they're still considered tenants but now you're talking about floors and floors and floors of different types of tenants and buildings and the shape and but there's still a building at the end of the day let's look into that so before i used to write all these classes underneath main let's let's be proper let me make a new class and we're going to call this building and this is where we're going to make our parent class now this is just a regular class and by default in kotlin it's final which means that it doesn't allow for subclasses to inherit from this now to make this inheritable and to make this an apparent class we can put a keyword right in front of this class keyword and this keyword is going to be open now we're open for business now other classes can inherit from this class because it's open to do so all right so now that we have a open building class let's start defining some properties that we would want our class or subclasses to retrieve when we build that relationship with the parent class so we have building as a template but what does every building have or should have so if i go into this class i can start thinking of that so what does every building have it should have you know at least one room so we can have a property call rooms and i'm going to make that a valve and i'm going to make that an int and then i'll have a default value of 0 and then i have another property called bathrooms because i don't want them making you guys use the restroom outside so i'll make that a double because like i discussed before a bathroom in a house standpoint can be considered a half or could be considered a 0.5 if it just has a toilet but if it has a shower and a toy then it's considered a full bathroom maybe in a building that it may just be the toilet considered as one bathrooms but i want to give the user the chance or the opportunity if they want to consider half a bathroom they can do that so i'll make that a double and i'll have this holding 0.0 then i will have color and i'll make that a var because we don't want to restrict the user to just one value for color it could change we can get a painter and they can change the the color to another another type so we'll make that a string and we'll have that empty and we can even have methods but besides that a building should have a owner so they handle the responsibilities of the actual building so let me just create a property at the top called var because the owner could change uh any real estate um property you know the owner could change if they want to choose to sell it or if it forecloses so i will have that as owner how far and let me get a string and it's going to be empty for now and then we can have a method that prints out the owner just a nice little welcome like hello i am the owner so we'll have a function owner prompt just so that people can know who's the owner open that up no parameters because we're just going to retrieve the owner value and we're going to just do a println open that up hello oh we're just going to do something basic the owner is and then we're going to have some string templating and we'll just put wrong one it should be dollar sign and boom now it's grabbing the owner from the properties and it's gonna print it out if it does have an owner so you probably think we're done but that is not the case see just like how the class needs an open keyword so that a subclass can retrieve um the properties we still have to make because by default you know a class is final in kotlin but the properties also need the open the open keyword for the subclasses to override the value if need be so i'm just going to put open in front of the properties that you want subclasses to be able to change so you can just do that and change that so i want all of these to be open so that if my subclasses want to change it or retrieve it it is possible so now everything's open everything's open for business now let's create a subclass that will be inheriting or building a relationship with the parent class of building so i know a house is a building we've been using in the previous examples but we're going to create that class a specific class just for house all right now to inherit or to make that relationship with the building parent class or base class all you got to do is put a colon right after the name of the class and put the name of the parent class which in this case is building and yes there isn't a constructor in the building but we still got to define you know a primary constructor so we could just put an empty constructor because we're not we don't have any parameters that we're accepting at this time but it is possible and we're going to look at that later but now we have the opportunity to start overriding properties so to start getting value so if you want to use a primary constructor from house to start defining some of the values that we took from the building template or the building based class parent class we could so now all we got to do is use the keyword override right and now we just need to use the properties that we define the building and we can override those values with whatever value that the user passes into the house constructor so let's do that so remember override and we have four properties so we're gonna have to create an override for each of these properties if need be all right so let's start with owner and file owner and boom the first mistake already but it's an intentional mistake as you guys can see there's a little squiggly that says file property cannot override a var property so we're able to see that there's a connection and the issue is that in building we define the property as a var right but in this case we can't just switch from a fire to a file that's going to cause a problem so we got to change this from a vowel to a var but the weird thing or not the weird thing it makes sense but if this was a vowel we're able to make this avar because the only difference between a var and a vowel is the fact that a vowel is not mutable which means we can't change the value right but it's not an issue if initially you know this is a you know we're not able to change the value and now we want it to be able to change the value but before if it was we were able to change the value and now we wanted to not be able to change the value that's going to cause a problem so you the original class or the parent class could have a file and then we can override it as a var that's possible because now all we want to do is you know apply that setter and that wouldn't be an issue so just think about that but now we have to override the other values so override and there was rules and that was a valve so we can do that in rooms and that is going to be an it and just so that it's cleaner let's put it each on one line and then we'll make the file for bathrooms and then we do one more final override for bar and this is gonna be color string all right so now we override all the values that the user passes in and this is taking reference to the building parent class and as you guys can see they're all grown up now meaning that we overrided the values based on what the user passes in in the house class and now we have access to it beautiful but look we also define a method in the building parent class so we can also override that as well because it's open so if you guys can see it gives me the option to override the owner prompts which i want to write it out by hand because it's better to do that so you guys can learn it oh it already you already built it intellij is too smart but look we could define our own prompt here we just do print online and we can do you um welcome from and then put the name of the owner that we passed into the constructor if i can spell welcome properly and now if i go into the main i can literally just make an instance of house passing the owner which i'm going to put as geo it's gonna be a nice little five bath or five room five or six point five bathroom it's gonna be a nice little group let me get green all or emeralds like some wizard of oz type thing but if i run this after i do the house owner prompt you guys will see the prompt i just created by overriding welcome from geo so by going to building originally you see the owner is owner but since i overrided the property and defined my own my own print statement inside it pretty much erased everything from here and just defined what i put which is awesome but what if we're okay with the original value so if we're okay with the original prompt we could just get the instance of the original base class version of it by using the keyword super and then using a dot on a prompt so now we have the original return value of the method of the original method so now we should expect it to return the owner is geo so if i go back to the main and i run that boom the owner is geo and that's just the magic of building the relationship with parent and subclasses using inheritance now if we want to use the template we could we have a template and now we can inherit as many times as we want within other classes and now we can even use um the original properties or the original method value or we can create our own by overwriting it we overrided the properties ourselves and we overrided the we overrided the uh method that we created so either or we can choose so that's the cool thing about inheritance all right so just as promised i created a constructor for the building parent class and i have the user passing in the market price and every building has a market price whether it's lower whether it's high it still has a price for someone to buy and live in or use so in this case i have a market price being taken in from the constructor and i have a niche that's printing out the current value when the person passes in the market price and then i have the regular properties and method that was originally a part of the building class so now that you guys seen this i also created the apartments class all right and this is going to be overriding all the properties that we originally had in the building class but also taken two additional uh properties that is specific to the apartment so we have a price that is double and we're not overriding it because it's only specific to the apartment class and we have a tenant's map this is going to be holding an ins to specify whether a person lives where what room the person lives in you know because in an apartment everybody has a room number that specifies you know the current location that the tenant is living in so it can be room 101 for the first floor or 205 for the second floor things like that and then we're passing in the price that is being passed into the apartment constructor because we're inheriting from the building we still have to pass in the value that um the building is expecting so like i said we have a primary constructor that is expecting a market price so the price that we're expecting from the apartment we can pass that to the building class primary constructor and then once the building class gets that it will print this right away with the price that was passed in through the primary constructor then the tenants i created a niche that will initialize this tenant property that i created specifically for the apartment class and it will equal the tenants that the user passed in and then from there i created a print tenant that's specific to only the apartment class and this will print out a tenant based on the id so i have a tutorial um that i went over maps and how to create a map and and arrays and stuff like that but to get the instance of a specific tenant i'm passing the id which is going to be the room number i could have used room number but in this case i'm using id and if it's not equal to no that means someone's living there so i just print out the current tenants um and because you don't wanna just print out all your tenants like you should only you know print out a specific tenant if the person knows the room number and if the person if that doesn't exist then that means there's no current tenant living there so i feel like this is a kind of realistic um scenario for apartment and you can add way more stuff there'll be a little project for you guys but this is my apartment class and this is what it can do and i'm gonna have the code on my um you know website which is tech with geo.com and i'll leave the link in the description but now if i go to the main i'll create an instance of apartments the owner is gonna be geocorp he's gonna have the apartment's gonna have 30 rooms 35.5 bathrooms you know there's one there's one bathroom in in the office that you know doesn't have a shower so um and then the color is gonna be yellow for the apartment complex the current price for the apartment is two hundred thousand and we're going to create one map like i said i went over um how to you know maps and you know arrays and the the basics of kotlin in another video i have a whole playlist actually if you guys want to look into that but i'm creating a map with just one tenant you know this is a fresh apartment and this tenant has a room number of one and his name is luke skywalker and then after that i am printing out a tenant so i'm passing in the id and if a person lives there then they'll print out you know who lives there so in this case it'll probably be luke skywalker because we only have one tenant so if i run this boom the current market price is 200 000.5 which you know we specified here and that's being printed out from the um the building class from the init the current market price as we pass in the market price from within the apartment class the price that we passed in is being passed through the primary constructive building class which we're inheriting from and the second print statement is coming from the print tenants which is current tenant is luke skywalker so if we go back to apartments it is doing this logic and it's seeing that there is a value within the map and it's printing out the current tenant living under that id or that room number now if i go back to maine and i put two or two and then i run this the market price is still going to be 20 000 because that's what we're passing in but it's saying that there is no current attending no current tenant living there because i mean if you check the map that we're passing in i mean there is no two um so if we try to see if someone's living at two then there's nobody living at two so this is how we can use you know inheritance it's it's pretty awesome um we can have this template but additionally we can have our own properties that's specific to the class that we made such as you know tenants and price and even price can be added to the building because like i said price is um every building has a price so whether someone is choosing to sell it or not so you guys can you know look at this on my website or you guys can um you know write it out and and rewatch this to see um you know how you can add your own properties and and logic to make this more robust so hope you guys enjoy the video alright so let's start talking about visibility in kotlin so before we were just making an instance of our apartment we're passing in values into the constructor and it will assign those values to the properties that we define in the class sounds correct but let's start thinking more realistic in a real realistic scenario type of thing in real world application so apartment has all these properties but when we use donational department you're able to see all the things that are currently or all the properties and methods that are currently inside of apartment in a real life scenario that's probably not a good thing you don't want things to be so visible right because i can just go in i can pick apartment.tenants i can make this equal to map of and now i have no idea who are my tenants because now my tenants are going to become an empty map now all my tenants i gotta go door by door and figure out who's who and that's not a good problem to have so one way to fix that as i erase this is to change the visibility of our properties we can go into apartments and there are four modifiers visibility modifiers in kotlin there's public which by default you know if you don't specify a modifier then everything is public which is visible to everything from everywhere and then there's private which means it's only visible where it's declared so as you guys can see that this there's this tenant's property declared in the apartment class and right now by default is public but if i set this to private now it's only can be seen or it can be changed from within this you know place that is declared which which is the apartment's class but if i go into my main and i try to do the same thing as i did before and use the dot notation to try to see you know properties to change tenants is not available i mean i can still print i guess it'll print out tenant i mean i'm still passing in the value for tenant which is this map of this map of luke skywalker but if i run this right and i have no access to tenants but i can still see what's going on in the background based on the information that i passed in or the data that i passed in from the constructor but i just can't change it after because i can't see it after i don't want it to be visible because of you know it can be changed from malicious intent or even by accident so then that is private you know you only want to be seen from where it was declared but then you have another thing see if i if i make one of these private and um first of all first rule you can't make you know an open and open um property private because then the subclasses won't be able to see it so that kind of defeats the point of you know making it open because it want to be seen from the declared class so that means even if i'm inheriting from building if i'm trying to make this private it won't see it so kotlin just telling you yeah nah that's not a good idea it's incompatible but there is another modifier called protected which means that it can only be seen from within the class that it was declared as well as subclasses so it's not blocking it from you know apartment which is a subclass of building um but now if i go and try to do the dart the dot notation again and i made owner protected so if i try to get owner it's it's not theirs it's not it's not letting me i can't i don't have access it's protected so visibility all right you got to think about the different scenarios of how you want things to be seen in your application and different scenarios on you know what what do you want users to be able to touch and in this case um i might want to create my own uh function that checks if that person is actually valid to change the the ownership to somebody else so you just got to think about that all right so the last visibility modifier is internal which means that is visible within the module and we talked about modules a little bit in my first kotlin tutorial uh module which is a kotlin in kotlin is just a set of column files compiled together so that can be you know maven projects gradle sets um intelligent model modules those are all modules and they're compiled together so really on a realistic setting unless you're um you know using modules or playing around with modules you won't really use this modifier too much but i just wanted to provide you guys you know context on what it actually does and you know if you do then you know internal if you only want it to be seen within the module then internal will be used so okay we have public which is by default you know everything is public in kotlin um and it's visible from everywhere then we have private which can only be you know viewed from within the um class and then we have protected which is viewed from within the class or where it's declared and the subclasses so if i make something protected in you know building you know apartment is inheriting from building so i can see it from here but i can't retrieve it from anywhere else or i can't set it or retrieve it from anywhere else and then this internal which is visible within the modules so you can just literally put you know let me see instead of private i can put internal you just put the internal keyboard and it's visible from only within the module so yeah i mean saying right now that it's recommending private but i just want to show you guys how internal looks like so thank you guys for watching all right so to begin the call and object oriented programming journey i talked about classes which is used as a blueprint for the creation of objects but we will now talk about another type of class which is the abstract class so first an abstract class cannot be used to create an instance of an object so like how we had before with this apartment class which holds you know different properties and methods we can't create an instance of it by passing in values in the constructor and you know making this a variable called apartment to hold you know the instance of that object it's not possible to abstract class so what's so special about an abstract class well let's create one first and then we'll talk about it step by step so first we'll just make another kotlin class we'll call it building details because i wanted to define some building details about the one that we created and then now to make this class abstract all you got to do is put the keyword abstract in front of it so now we have an abstract class so now we can start defining some abstract class properties and methods now by default any property in abstract class is non-abstract so to override it we either have to declare it as open or we can create it as abstract which by default abstract properties and methods are open so let's create an abstract property so we're going to call this abstract and we're going to make this a val with the floor amounts of the building because we want to know how many floors the building has and we'll make this an ins so the first thing about abstract property or method is that they cannot be implemented i can't make this equal to zero to give it a default value that's not possible because it's abstract only the subclass that's inheriting from building details can implement it so if something is or if a subclass is inheriting from building details it has to implement the abstract properties and methods you cannot go without you know implementing them so let's create another abstract or we let's use the abstract keyword again but we're going to use it to make a method so we're going to make this fun and then we're going to make building props and this is going to be used to you know welcome guests into the building but again this can't be implemented because it is abstract all right but an asteroid class can contain abstract and non-abstract properties so we could have um let me see once again let's do var window type and we're going to make this a string and maybe his impact and by default we just made this empty this is possible just because we're in the abstract class like i said before you know all properties inside the class properties by default are non-abstract so to overwrite it or if you want to overwrite it we have to place it as open but because it's non-abstract it's not using the abstract keyword we could implement it so for now i'm just make it as an empty string and whoever inherited inherits from this building details class can implement it from or can override it from the original empty string so an abstract class is perfect if you know you're going to have subclasses that will need to define certain properties and methods for sure because by default you have to implement the abstract properties and method then let's make sure that you know i'm telling the truth let's go into building right and to inherit from an abstract class you just do like we did before within open you know with a parent class you're just doing the colon and then you put the name of the file you're trying to inherit from or the class that you're trying to inherit from so in this case we want to inherit from this abstract class building details now you're going to see some swiggy lines so first let's put in the primary constructor but you're going to see some swiggly lines up here from intellij saying that class building is not abstract and does not implement abstract base class member floor amounts and also um you know because building details have this building prompt that's also abstract we need to implement that as well now it's saying it's not abstract as well now if we made this abstract we wouldn't have to implement it because it's also abstract so we can also have abstract properties inside of it and non-abstract properties so that's why these still work and apartments still work because it's you know it's still inheriting from building so everything would still work but now that it's not now that the apartment is not abstract it will have to implement the things that you know building is inheriting from um which is everything from building detail so it'll pretty much start a chain so let's remove this as abstract let's make this open and let's just implement what they want us to implement so let's override the floor mount and we're not going to use the getters and setters we're just going to make this equal to zero as well whoops and we're going to override the building prompt as well and we're just going to print out welcome to and then we're just going to do welcome to my building so boom now you know for sure and this error goes away that if you want to have a class that holds properties and methods that you know whoever inherits from it will for sure implement those properties and methods or have to implement those properties and methods an abstract class is perfect for that because it creates a dependency that can't be ignored but the thing is a subclass can only have one abstract class so we can't define multiple azure class classes and use multiple and just put a comma and just use another one you can only have one so make sure whatever you need is within that that uh the abstract class but like i've shown before there could be a chaining type type of thing so if necessary that can be done with multiple abstract classes all right so we got the value from building details and we overridden it and implemented it just like the abstract class wanted or the abstract property and method wanted but what if we want to use it in apartment the apartment class well we still got to go through the same process so to get the floor mount we could just override it again so we're going to do override file and then i'm just put a comma override file floor mount right and then we got to go back into our you know instance of apartment and you know add that property or that parameter but then let's make that prompt for the building as well so let's override all rights and then let's um put the function of building and you know we're just going to use the instance that building prop originally have which is welcome to my building um and if you want to add your own you know name to apartments the apartment primary constructor and then use that name like welcome to my beautiful estates or whatever the name of your uh apartment is you know apartments have names then you can do that as well but in this case i'm just going to use the version that i created in my parent class and now we should be good now i just got to go back to my main and i believe i put it right here so i just got to put how many floors does this apartment have i'm going to define it as three and look floor mounts is three price is twenty thousand um you guys could do this for my equal if you want to to make sure as if if your constructor is getting big like this you can do that as well just to make sure you're putting in the right values but you know luckily intellij is smart enough so i can see the name right next to it but if you want to make it clean you could put the name of the parameter up to you but let's run this that's to apartment taught building prompt run this and then boom welcome to my building it takes the because we're using super like we learned before or let me find the file because we're using super artistic we're just taking the version of the parent class and just to show that i'm getting the proper um floor amount let me just print that out as well and then and then a parts dot floor mounts and we can make this private if we wanted to or protected whatever you guys want but just for sure you can see that you know i'm passing is passing in three as floor amounts and i'm able to get that value now if you don't want to be able to retrieve you know floor mount like we just did we can just go to building details and we can just make this floor amounts protected so now only the subclasses and the place that is created can see floor mount so if i go back to the main now it's showing the rest quickly can i access floor mount it is protected um and you know you shouldn't make it private because it's open and you know other classes gotta override it so private is not option so protected is perfect for this case when you're going to have sub classes that inherit from building details and building so this is perfect but now we can't do this anymore but building prompt is still an option and we're still able to pass in the value to floor mount so this is abstract classes this is how you can use them remember there you can only inherit from one abstract class but you can create this chaining effect so if possible like i showed before you can have this abstract class you can make building abstract and then you can have class inherit from building and then you have to implement the two abstract properties or whatever abstract properties that you define in apartments and i keep going and going and going and going until you have you know your your class that you want to inherit all those properties that you created so you just got to figure out do you need a abstract class to hold properties and methods that you want for sure to be implemented so think about it and play with it alright so in the last video i went over abstract classes but now we'll start talking about interfaces and how to create one so what is an interface an interface is a type where we can implement abstract methods properties and method implementations so it sounds pretty much very similar to an abstract class by default everything defined in interface is abstract unlike the abstract class where everything is by default non-abstract so let's create one let's create a maintenance interface this is so that we can check the current status of our building in regards to maintenance for either a tenant or maybe use half a regular house and you want to check the status of a specific problem that you're having so let's create new columns class slash file and then we'll just do building maintenance i hope i spelled that properly and now we're going to choose interface and boom now we have an interface it's literally just the interface keyword and the name of the interface so now let's start defining some properties and you know functions so the first one i'm gonna have is going to be called file and then i'm going to put may to nance status so it can either be you know pending or it can be um completed you know so that's a string but like i said by default anything created in an interface is abstract so if i try to assign it as empty string it's not possible because by default you know it's not allowed in interfaces because it's abstract whoever the subclass that inherits from this interface we'll be able to implement it by overriding it and because abstract is open so um you'll be able to override it so before i talked about how interfaces and abstract classes are very similar but they're not exactly the same like i said properties in the interface by default is abstract um and by default after keywords are open by default a class can extend many interfaces but like i discussed in the previous video an abstract class can only be inherited once but we can create a chaining effects a training effect if needed an interface does not store state so as you guys saw that you know we tried to give it a value but because it's abstract you know it doesn't allow us to do it but if you know you're going to return the same value all the time if you really really want to you know return a specific value you could use the getter that we learned in the in the getter and setter video so you could just do this or open the string you could do this and set the uh getter to an empty string but for the most part you know you'll just be overriding it but if you really want to have a you know value then you use the getter because an interface is not a class it's a type it does not have a primary constructor so we can't just put you know parentheses over here and accept the value because it's not going to allow for that you know interfaces do not have a constructor all right so now we'll start defining some methods within this interface so let's create a method that will print out the status of you know maintenance so just put prompt status and now we created an abstract method within this interface but we are allowed to have normal methods so we just got to open it up provided a body and now this is just a normal method it's not abstract and it doesn't need to be overwritten so let's just create a prompt and we're going to print out the status we're just going to put status and then use some string templating and i'm pretty sure having misspellings somewhere maintenance i've just got i'm just gonna copy this maintenance status all right cool so now when the maintenance that is overwritten if we try to use prompt status then it'll print out the current status but because it is not open we cannot over it overwrite it so if we do want to overwrite it we could put open um but if you don't and you just want to print out the prompt status then you can just do it based on the main standards that you uh override the value that you override me and the status with so now let's see it in action let's inherit this so we're going to go all the way back to building we're going to put a comma after this we're going to put building maintenance it's going to show us quickly like before we're saying that you have some abstract property that needs to be implemented so we can just override it um and in this building i am going to place it right under here open or actually override maintenance status and we use the getter but we're not we're just going to allow for it to be overwritten so it's going to say that you need to initialize it programming is equal to an empty stream but now this is accessible in apartment so if we want to go into apartment because it's inheriting from building so this is this is the effect so we got this interface which is you know put into building because we want to know the maintenance of this building the current status so we inherit from this interface now you see that everything is lit up lit enough because we inherited building maintenance within building so now all those properties are available within here but because apartment is inheriting from building apartment has access to to those properties as well so if we want to pass that in um we can override maintenance status put that and then go back into the main and then we can put it right before here so i'll just put pending and put a comma so now if i do apartment dot building prompt no not building prompt dot let me see maintenance now the prompt status there it is and i'm just going to print um comment out these print statements if i run this i should expect to see status and then it should have the status that we provided which is pending so on on a bigger scale we'll probably make a map um which you know would have another map within this with each tenant and each tenant will have their own version of maintenance status because everybody in the building should have you know their own you know status uh for an apartment because every every apartment room has you know different problems so in a bigger scale you probably take the maintenance status that we're you know taking from the building maintenance interface and provided inside of this map and we could probably make another object called tenants and pass that in um inside of a map so there's a lot of things that you can do but on a simple scale that's pretty much you know how to use an interface how to create properties some can be either you know abstract or you can have a method that is non-abstract um that can be overwritten but it can have a default value that we use which we're using now which is taking the maintenance status that we override um and the path the value that's being passed from apartments is being used to print out the status here so there's a lot of ways you can play around with interfaces but yeah and you're not just restricted to just using one interface you can use many so now let's create another interface that will hold information properties methods regarding building staff so we can know you know who works for you know our apartment and you know what's their id and what's their name so we'll create another interface and kotlin class file i'm going to call this building staff make it as interface now let's start defining some properties so we're going to make this a map so let's do val and then we're gonna do staff or building staff we're gonna make this a map of string and or actually we'll just do id and the name so we'll do it and string if you guys want to make it more difficult you could create another object called employee and have that as you know the second option instead of string and then you can dive in deeper on you know employee so you can create properties for the employee like name um you know description on what they do their role so that's an option but i'm just going to keep it simple and have an int for id and string for the name um and yeah that'll be it and that is our first you know abstract property because by default like i said before you know all properties by default in an interface is abstract so now i'll just create another um or i create a method that will tell me how many staff members i have so we'll just create a function called amounts of staff oop capital s case and then open up parentheses and open up the body because we are able to have abstract and non-abstract methods so in this case i want to create a method with a implementation so i'll just create a prompt that will take the size of my building staff and return it so we'll just do um amounts amounts of staff is and then do some string templating open that up and then building staff dot size and now whenever we override this building staff with some you know members of our staff or if there's no staff then when we use the amount of staff when we print this out it'll just print out the size of the staff and then you could create another method and that'll be homework create another method that you know iterates to the staff or it can do like i did before get the index of the staff member and then use it to print out the staff or if there's no staff then it'll print out there is no staff by the id and that's my little homework for you but all right now we'll go back into building and now that we have another interface all we got to do is put in a comma and then put the name of the interface that we just created which is building a building staff alright and you are able to you know inherit multiple interfaces but the swiggly line is saying okay you have some abstract properties or an abstract method that needs to be implemented so we'll just create an override here for building staff um all right and then we'll just make this an empty map because right now we don't want to implement it here we want to implement it in our next subclass so we'll just have it empty here and like i said before you can you can inherit multiple interfaces but you can only inherit one abstract class so now let's go to our final subclass that's inheriting building and here i will allow for the uh the creation of employees and right here i want to create another private var and i'm going to call this employees or staff it's called staff and i'm going to make this a map of and then i'm gonna make this an int and a string all right and then this dot but i gotta make you know staff so i can pass it in but uh make that equal to staff and we should be good to go all i got to do is add it let me make it right next to this tenants so i'll just call it staff and i'll make this a map and it'll be the same thing in a string now if i go back into our main i now have to implement that map and i'm just gonna make one one person so one two um and i'm gonna name the staff member bobby bobby boss man i'm really bad at making fake names but um then we have access to um even though we don't have we didn't override or we don't have the value here we still have access to the interface because we're inheriting from building and if you go back to building that's inheriting from building staff if you go back to building staff i still have this amount of staff i have access to this so if i go to apartment dot and then i should be able to see amount of staff and go down select that now if i run this into me i'll keep this so you guys should see that still see the prompt status um so now i should see the current value is 20 000 which is the first um print from our initial tutorial but then there's the status which is pending which is the second print and then there's the amount of staff which is zero wait a minute we made a mistake and i'm gonna like keep this in because i wanna know why exactly is this not working so we have staff and we're passing that to staff right here making this step equal to staff and ah okay we didn't we didn't override we did not override to actually assign its uh staff so we can do that um here let's override and it'll just be building staff so instead of it being staffed it'll be building staff and now we should be good to go now if we run this from the main it should say one so that that's a very simple mistake to make you do have to override the values that you're inheriting from um and because there's no implementation yet um so that was the mistake to make but now you guys know you can have multiple interfaces and you know pretty much created that chaining effect we have building which inherits from building interface so we have our properties in our method go back to building um you know you can override it you know put some default values in here but then if i go back to apartment i can override that again and place in the value that i want to use and then i can assign that to you know my internal property which is staff so but that's just going to be equal to the building staff so thank you guys for watching all right so let's start talking about nested classes this class is getting pretty pretty big so i'm going to make another class called skyscraper we're actually no we're going to do office building we're going to make an office building um you know for like your dentist and stuff um so let's do new class um and then we're gonna name this office building um and then we're going to inherit from building open up that now it's expecting market price so we'll just open up a constructor here so we can pass in what they expect honestly i'm just going to go to apartments and take all the overridden values so i'm going to just take these four and then open this up paste that in and then we'll have a um market price oop market price and then we're going to make this a double right so now the person can pass the market price and that market price will be passed into building because they're expecting a market price for the building um and then i also want to make a map for the list of you know corporations that are in this or a map of corporations that are in this uh building and it's going to have the room number that you know the office number that that corporation is you know using so we're going to do corporations and then we're going to do open that up oh map open that up make that int and make that a string so now and here we're going to make a private variable and we're going to make that equal to corporations and that is going to be a map and an int and a string um and then we're just gonna go into this init block so that we can initialize corporations so it's gonna be disk start corporations it's equal to corporations so i should take that away all right now we can start talking a little bit about the uh nested class to create an essay class we simply just need to place a class within a class as the name implies it's a nested class so we added office building class and now we want to create another class called storage which will be a class specific to storing items based on the corporations that's in the office building so let's just put that class keyword use storage um and then open up some brackets or curly braces um and then now we can start defining some properties within this nested class now the interesting thing about nested class is yes it's within another class but we do not have access to properties within the enclosing class so just because we have this class storage inside of office building we do not have access to corporations or you know any of the properties that we define in this enclosing class of office building so storage is its own space we can create our own property specifically for storage so let's call this new property i'm going to make it so far storage items and is going to be um we're just going to make it a map actually no i'll pass the value through the storage um primary constructor which we could do it's a class within the class but it still does have its own primary constructor and we're going to make this file storage items and then we're going to make this a map i still want to make it a string or an int and a string because we're going to use the corporation's room number and we're going to use string to pass in whatever item it is you could make this a list or another map or yeah a list or map if you want to but i'm just going to make a string so i can pass in one value and then okay then i just use a net and i actually do need to make another var storage item map and i am going to make this a map and then i'm gonna make this private actually because i don't want to give people access to do it you can make another function that you know puts values into a map if need be but that had to be a mutable map and then make this a string and then you know this dot storage item map equals storage items now you're probably wondering okay how do i get access of this class within the class all right so let's go back to our main and it's not just as easy as just implementing you know this office building which is holding our um nested class and just doing office building dots it's not even going to be seen i can put s and intellij or kotlin doesn't see it but what i need to do is get the class that's enclosing it so in this case it's office building then use dot notation to see you know what's within that class and now i can get storage and now i can use that primary constructor to pass in values that you know i define so in this case i created a map parameter for the storage items and now i can do or create a map of you know different corporations so now i'm going to do 101 and that's going to be to star bees boom so now we have office building which is referencing back to the storage um nested class that we created and through this nested class i am passing a map and with that map i am storing um you know the storage items to um the private map that i created within my omnip block so now i want to create a function that will allow us to print out you know what the corporation is storing so we'll do a fun and then print storage item um so then we'll just go to um pass in a index or a room number and that's going to be an end and with that ends we'll check if the map has oh let me fix this spelling we'll check if that you know room number has anything in the storage so we'll just print out or we'll do if statement first that will check if storage item map at the room number is not equal to no to check if it actually does have a value and if it's not equal to node then i can print out the storage item map add that remember else then print out no storage item now if i go back to the main and i do and i'm gonna make because this is an instance of storage right here so i can put this as a variable that holds that instance of storage i'm gonna make that equal to that and then i can do storage dot uh print storage items and then i can pass in 101 because that's within the map right here and let me just come with these up now if i run this obviously it's going to print out the current value of each instance that i create of building because i have a nit in building that print out the market price but besides that you are able to see that you know i have a map that's printing out starby's or whatever item that i'm storing um in this storage so it's its own space storage is his own space it's nested um but i can still have an instance of it and i can you know pass it to this i can have the storage variable that holds the instance of storage just by using the enclosing um class and using dot notation to retrieve that instance or that that class of storage and then create an instance of it by passing in the necessary parameters if you are using any values in the primary constructor or you can use the empty primary constructor and just because it's enclosed in this you know parent class does not mean that you can't um you know use it like a regular class by giving it a parameter and um you know using that value in the constructor and all that so this is an example of a message class now you're probably wondering what if we do want to use the properties that the enclosing class has to offer so what if we do want access to this corporation property from within this nested class of storage well there is another keyword that we can use called enter and all we got to do is plug that from you know in front of the nested class declaration and now we have access to corporations so now to use that all we got to do is so first now that i have access to the property from within the enclosing class i want to check if corporations run up the room number and storage item map room number both is not null so that i can print out the name of the corporation and that i can print out the item that you know that that corporation has so i'll just add in an and which i believe that's two ampere cents i think that's how you that's what they're called ampersand and then we'll do corporations and as you guys can see i can see it now before when i took enter i didn't have enter corporations was not showing up because i didn't have access to it but with the inner keyword i do and now it's not going to get mad at me and then i can use the room number and as long as that is not equal to no right we should be good to go so i'll print out actually not just make it in one so i print out the corporations and use that room number to get the string value and then i just you know actually i'll plug this into a string so i can put in a colon make it a little bit cleaner um and then i'll use a dollar sign string template and open that up put a little coal in between oh i missed a dollar then plug that in there and now we should be good to go all right so we got to make sure that the map is you know holding a specific um room number for that corporation and that corporation has that item within the storage so that they're both not know so now if i go back into the application that but now we gotta um actually pass in a constructor and honestly we already have an instance right here so all we gotta do made a mistake there open that back up all we got to do is let me comment this out um and now i can just take this instance of office building and now i have access to storage and i can just pass in um this map right here actually passing the exact map i'm going to pass in the storage value or the storage items and we're going to use that same room number and we're just gonna have some coffee inside storage now if i want to print out the storage i can now use office building and then use dot print storage item pretty neat now if i run this for 101 now it's saying starbees i got some coffee because now i'm passing in first the corporations the room number at 101 is starby's and now i have an item within the storage for room number 101 to hold some coffee that's a plane flies by my house so now if i go back into the office building i have access to corporations from within this inner class i am passing that storage items to hold within this private storage item map and then i'm just printing checking if storage item and corporation is not know for that room number and then if it's not no for both of them then that means they're correct and i can print it out obviously you can make that that if statement more we can make it more strict um but um we're just gonna keep it simple here so yeah that's that's the inner class so if you guys do want properties or access their properties from your enclosing class you can do that thanks for watching now it's time to start talking about another type of class and this class is called the data class if you have any experience with java object oriented program you probably know about pojos which is acronym for plain old java objects it's a class to use to hold data so now kotlin implemented a thing called data class which will help us create a class to hold data the awesome thing about data classes is that the compiler automatically generates utility functions for the class this includes equals hash code tostring component n and copy and we'll go through each one of these utility functions one by one so we can see them in action to create a data class all we got to do is go to source new and then cotton class and then we'll just make one for employee and then i'm gonna create a data class so i go down and select data class and boom now we have a data class the only difference between the declaration of a data class in the class so far is the keyword data in front of class but there are some rules to create a data class first you have to use at least one parameter in the primary constructor so right now it's showing a red wiggly line because it wants at least one primary constructor parameter parameters passed in the primary constructor has to be marked by val or var so we'll just make a vowel called name and that's going to be a string and that will meet that criteria last thing data classes cannot be abstract open sealed or inner so if you want any of those um type of you know declarations that can't be done with that class like i said that class is used to hold data so if you want it to be abstract where people can inherit from it that's what that's not what a data class is used for um so it can't be open as well things can't inherit from it uh and we're gonna talk about sales classes later but it can't be a civil class and it cannot contain or it cannot be an inner class so now um we have a data class that takes in the name which is going to be a string now we'll see some of the utility functions that a data class comes with on compilation so let's go back to our main and let's comment all of this out and start from right at the bottom and we just created an employee.class so we'll create an instance of employee equals employee oops all right cool and now it's expecting the name in the param in the constructor in the primary constructor so we'll just use giovanni so now we'll start using the utility functions that a data class comes with on compilation the first utility function we're going to use is the tostring the tostring returns the string form of the data class so currently we just have one parameter that we're passing into the data class and is storing that as name and that value is giovanni so let's see this in string form if we do a println and we do employee which is holding the instance of employee with my name and do two string and run that we should see the string version of my employee using my name so it's gonna be employee and the name is equal to giovanni so that's pretty cool if you have a instance of an object and you wanted to see what's under the hood in a very nice to see format the tostring would work so that's one utility function that comes with the data class by default and let me just go into i believe it's in building and take this println out all right so the next utility function that that class come with is the dot copy which just as the name implies the copy function just creates a copy of the object that you're trying to create a copy of so let's see this as an airplane passes by my house val employee 2 which is going to be the second instance of an employee and it's going to be equal to employee dot copy now we're creating a copy of employee but we got to pass in the parameters for the employees so and there's only one parameter for employee at this moment which is just a name we'll just pass in a new name which is going to be jim which is pretty cool now we can create copies of objects without having to to uh you know create a whole new you know instance of it like now we can just take that instance of employee and just use the dot copy pretty convenient and it's pretty much saying right now with squiggly is that you know we're not using the name arguments so we can just use name and equal and that should go away and now we can just do print and land and do employee 2.2 strings to see how it looks under the hood in a very nice format and then boom employee the first one's gonna be giovanni because this one and the second one is gonna be employee name equal to jim which is this one so now we have a copy but with a different value awesome another utility function that we can use is the hash code function which returns the hash value of the object so if two objects have the same hash value they are equal so let's see what the hash value for employee one is and we'll just print that right after so we're gonna make a nice little prompt hash value or hashcode of employee one and we'll just do string templating and we'll take the instance of employee and do hash code and boom and then we're just gonna copy this put that right out under employee 2 change the prompt just a little bit and then we're going to go to employee 2.hashcode so we can get the instance of this hashcode now if you run this we will see boom employee one has this long hash code and employee two has this hash code so two different hash codes so that means they're not the same object and there's an easy way to you know check if an object is equal so that takes us to our next utility function which is the equals function and this takes in an object to compare with another object to see if they have the same hash value if they do have the same hash value then it returns true if not then it returns false so we can just make another println there's a lot of prints going on but it's just a c to output something so you guys can see so we'll take the instance of employee then use dot notation to see you know the functions or the methods that you know that class provides and that is the equals and then we'll pass in the object that we're trying to compare it to which is going to be employed to and now and what is this recipe and we could use it equal equal to compare as well but we're going to do the dot equals and we're gonna run this and then boom it's false because we can see here that this hash code and this hash code is different so they're not equal to each other so it returns false when is it going to be true when is dot equals ever going to return true and like i said it has to be equal where it has to be the same hash code so when is an instance where an object can be the same let's see it as an action so let's let's create another instance of employee and let's do employee three because it's gonna be the third employee probably should make a more you know specific name but employee three and we're gonna make this equal to the copy of employee 1 but we're not going to pass in any values we're not going to pass in a new name because that would change the object we're going to make it an exact copy now we will pass employee three which is a copy of employee and we're going to run this and as you will see it is now true so this is an instance right made by accident you created a copy and um it's the same object this is an example of the same object being checked to see if it is the same object and in this case it is true because there's an age limit to whether a person could be an employee or not so that would have to be put onto the application regardless all right and that's going to be an int let's go back to the main and let's use component n so by default component n is generated for each parameter like i said for the structuring so let's do that let's extract the parameters that we have for an instance of an object we're gonna do name age and that's gonna equal to employee and this is probably looking ridiculous but let's do this so we can print it out and see what's exactly happening if i can split preliminary let's do employee name is name and age is age and we'll run this and be amazed so look an employee name is giovanni and ages 20 like how right so with the component n that is generated for each parameter we have the ability to destructure which means we can extract them and with this notation right here we can extract each parameter if we put in parentheses and pass in the name of the parameters we are now extracting those values from this instance of employee and assigning it to these variables so now name and age is the name and age in this employee so now we can just use that anywhere that we like because we just extracted because of the component n generated for each parameter so that class has come with so many utility functions that will be necessary for the stored state that we have for each instance of a data class so that's one thing you got to think about if you want a class to store the states or that's going to hold data the data class is perfect now if you want something that needs to be inherited from or you want to still class or inner class then a data class is not for you but if you know you're going to you know store some data and you're going to need some of these utility functions then the data data class is perfect for you all right so all right so let's okay all right so the last class we'll be talking about in this series is called the sealed class a sealed class allows for more control during inheritance it forces a single type from a limited set known at compile time which is called restricted hierarchies sealed classes restrict the ability to create subclasses so let's create a still clash let's do new and then kotlin class files and we'll call this booking and then we'll go down and select subclass and boom we now have a still class now a subclass cannot be instantiated and it's abstract by itself constructors in a sealed class is private by default it cannot be open which i mean can be overridden or not overwritten but there is no properties to override or methods and it can either be protected or private so it's not public the subclasses of a sealed class if you want subclasses must be in the same file so let's create another class called owner and this is going to be within the same file because we want to make it a subclass of booking make a valid id at the end as a plane passes by my house you're going to hear that a lot during this series but then it's going to be booking because we're inheriting from it then we're going to make another class called resident this is also going to be a subclass so it has to be within this class and i want the name and the room number that's gonna be it and it's gonna inherit from booking and now we're gonna make an object for non-residents so guest and this is going to inherit from booking all right so a silk class really shines when we start using wind expressions because of the restrictions set by silk class we know what to expect when we use one expression with a still type the compiler will get angry if a parameter not handled by the when is added so with a cell class we should expect we should know what to expect because of the restrictions especially with you know everything by default being private and protected so let's make a one expression so let's call it fun and let's call it front desk and there's e with the type booking and we're going to make a return to int where that wants to return the id or uh room number based on you know the booking so if we have an instance of owner residence we can return the id of the owner or we can return the residence room number and if it's a guest then we'll just throw like zero meaning that they're a non-resident or they're not even the owner of the establishment so let's do that let's create a win and we're going to take in the value from the function and we're going to open up this one expression and it's going to be if it is owner then we can turn e the id because that is the value that we're passing into owner so we should have an instance of that because if it is of type owner then you know e will be able to retrieve the id and is resonant it is a type of residence so because we have an instance of room number we can do uh e dot room number then the last the last type of booking that we have is non-resident and before i do that let me just erase that what's the error i was saying besides this yes see look it says one expression must be exhaustive add necessary non-resident branch or else branch so it gives us the option whether we want to use else or we want to use all the possibilities of booking because everything has to be within this class so it knows exactly what needs to be used it doesn't expect any outside influences because anything that inherits from booking has to be within this class there's so many restrictions with the civil class so that's where this really shines so now for the final case it's not resident if it is non-residents then we'll just return zero so now when is not angry because we don't even need else because we exhausted all possibilities of this expression so now when i want to front when the front desk passes in the booking type it would check whether it's the owner if it's the owner returns id if it's the resident then it'll return the room number of the resident and let me remove this comma and if it's a non-resident then we just return zero they're just a guess so this is a still class if you want things to be restrictive no constructor or primary constructor you can still have a constructor but it's going to be protected or private um and you want to use or let me take this other comma out it's a habit but if you want to have a specific or restricted class this is perfect of specific types and subclasses that's going to be all within one class so hope you guys enjoyed that alright so in kotlin we have a thing called a singleton object a singleton object is an object that has one instance for the entire project for example we may use a simultaneous object to store utility methods to use throughout the entire application and you do not want to create a new instance every time just for you know helper functions so we don't have to recreate an object or create an instance of an object like employee for giovanni and jim and and all that we can just have one instance of that object for the entire application so this is perfect for things like utility objects which have helper functions that can be used from throughout the application so let's create one for you know management we'll have this new utility object singleton object and we're going to call it that building go down to object and now we have a singleton object and now we can start creating some properties that we can use from anywhere so now let's make a property that holds the right amount for a standard rent fee so let's do a vow standard fee and it's going to be a double we're gonna make this equal to a thousand and what is this squiggly saying probably standard fees never used we are going to then we're gonna make one for deluxe so file deluxe fee as double oops and we're gonna make this double actually so we're gonna make it two thousand perfect now what if we want to return a you know value for the amount that rent will cost for a year or a certain amount of months we'll have a parameter that's taking the amount of months that we want to see and then we'll return the value times that you know type so we'll just do fun cost of deluxe for all right let's do rates luxury and then we're going to take in the months as the parameter and we're going to do end open it up and then we'll just return rant amounts times it by the months or my bad deluxe fee and we're gonna make this return a double and that's only if months is greater than zero so we don't want anyone passing in you know a negative value that that will cause problems so we'll have that check i've got to put a open brackets right here or curly braces right here and then we can have an else and return zero if you know anything else perfect if the month is greater than zero so i'm assuming one then we can just return deluxe fee times the amount of months if not then we'll just return 0.0 um or you could just return an exception or throw an exception but in this case i'll just return zero and we'll do the same thing for the cost of the standard fee in a realistic scenario you'll probably have you know a list and then you just check for the type but we'll just do it this way and then cost and then months and then standard fee so now we have two functions we have one that returned the luxury based on a months that you you know intend on you know staying and then the cost of the standard rate depending on the cost or the months that you intend on staying and would times that against the standard fee so now we have a building utility singleton object and now this is going to be used from anywhere or can be used anywhere within the application so let's go into our main and we're just going to come all this out again and then let's print out what if we want to stay or actually first of all let's make this private by the way because we don't want this to be changed from the outside we only want to be seen within this building u2 we should only be able to change the rates nobody else um so then we're just going to just print len now we make a call to the object we don't have to create an instance but we can just take a reference of the building youtube and then use dots notation to see what we have inside of building u2 and we can see the cost of the luxury and cost of the standard rate i want to see how long or how much it will cost to stay at a deluxe room or in a deluxe room for 12 months now let me see let's put this in string notation or string template and make a nice little prompt so the deluxe rate to stay for 12 months is and put a little dollar sign and then close that and then actually put an actual dollar sign in front of it so two dollar signs um and then we should be good to go so now we know or now we can see how much it will cost to stay at our apartment or hotel whatever building for 12 months using the utility object boom so to stay at the stay at a uh building at a deluxe rate it will cost 24 000 12 000 or twelve times two thousand is twenty four thousand quick maps and i just copy that and we'll do the same thing for the standard and that's going to be 12 months the standard rate to stay for 12 months is and then let's see how much that would cost it should be half so 12 000. so isn't that cool like we don't have to create a new instance of a class to get some functionality we can create a singleton object that is the same throughout the whole application we only have to make um you know one instance of it and we can just reference it by using dot notation and we can have access to whatever utility functions that we need or that we created so this is perfect and we do have the option to inherit so we could i mean we're not gonna do this but look we have the option to inherit if needed we can pass in the market price but obviously in this case it wouldn't be um it wouldn't make sense to inherit from building but if you do have um you know a management class we can create a new management class and inherit from management and you know maybe they have their own um implementations that we need to inherit or override such as you know maybe management have a discount functionality uh maybe place contacts inside of it for executives or manage or other um hotels or buildings so things like that just use your creativity build those relationships and you know just code so in colin if you want to create a function or a property that can be called without the instance of the class you can use the thing called a companion object within the class so by creating a companion object you can access the properties by only using the class name so let's see this in action you already have an instance of office building right here let's go into office building and let's create a companion object so to create a companion object you use the keyword companion and followed by the keyword object and now we can just open up some brackets we could give it a name like boss or bossman or data but it's not necessary it's optional and usually i don't put a name so now we have our own space to start you know declaring some properties or a method or function that we can call without having to make an instance of this whole office building class so in a realistic setting like when i'm working and i'm using kotlin this is usually where i put my logger or i might put some constants in here and i can just retrieve the constant if need be or i can you know have an instance of that logger but um for now i'm just going to make a simple function called office building prompts and put a parentheses and i'm going to make this equal to welcome to the office building oops that is not equal so now i want to make a call to this office building prompt it's just going to return welcome to the office building so let's go into the main and i have instances of office building right here but is not necessary when using the companion object because we don't need an instance of a or we don't need the instance of the class to use things within the companion object we just got to use the class name itself so let's do a println um and let's get office building which is the name and then we can do office building prompt which if we go back to office building is within the companion object and if we run this welcome to the office building and like i said before even sometimes you may can or you can define constants in here so we can just do const file i'm just gonna make this pie um or let's see what's something that's important and i mean you could do like i don't know like uh square feet or something or but i'm just gonna do something ridiculous and just do pi 3.14 and if i want to get that's then i can do office building pie boom and print that out so if you want to give access to something that doesn't require an instance of the object itself then you can just place it inside of the companion object and all you would need is to get use the class name and some dot notation and you'll be able to retrieve things within the companion object so just think about it in real world scenarios you can use that to put in your logger define some constants and you know stuff like that something that you want to be accessible so hope you guys understand what a companion object is