 Okay, welcome to introduction to computer science a that is AP computer science a let's let's qualify that or clarify that one AP Computer science a this is unit to a I'm dividing this unit into two different parts because there's about the first half Is about objects in general the second half is about objects, but string objects So we'll make those in two different videos My name is Christian Thompson, and you can find me on Twitter at at Tokyo Ed Tech So let's get started first thing I'm gonna do is I'm going to Give my class a name now if you haven't seen unit one I would go back and watch that first, so I'm gonna call this class units To and I'm gonna put an a there because this is a and as I mentioned before I'm gonna use this format for my braces and I'm going to make my public static void main and String oops string Args and this is just to get myself started So I have something to do Now I'm gonna save this and you'll notice it thankfully already does unit to a Java Remember these have to be the same and I am using utf 8. I'm gonna go on to my Computer somewhere when I find my desktop and go to my intro videos And I'm gonna call this unit to a and save it there Okay, now I've also got as I prepared for this particular lesson a class called dog dot Java And so let me explain. What are we looking at? So in unit 2.1? Basically, we're looking at classes class variables and class Constructors, and I'll put these eventually I'll get these files into the description down below So a class And and an instance of a class. This is what we need to look at Java is an object-oriented language So everything is a class. What what is a class? Well, the definition is that a class is a formal implementation or blueprint of the attributes and behaviors of an object Very very helpful So what's an object an object is a specific instance of a class with defined attributes So we're gonna be using looking at today or using as an example is a class called dog So what we're doing is we're defining What a dog is and what our dog's attributes are and what a dog can do and so that's where we're at right now So I've already written this and again, I said I will share this eventually So what I've got is I've got a private and I'll talk about that in a minute string And it's called name because our dog is going to have a name But since we don't know what the dog's name is the beginning We're just gonna put blank quotation marks and then we're also gonna have a private int for the dog's age And we're gonna say zero because we don't know the dog's age at this point Okay, so in previous examples, you've seen a public so public static void main for example So in this case, you'll see private So what private means is anyone outside the class cannot directly access This variable, okay, so what that means is here where I'm in class dog down here. I can access These variables name and age however over here in unit 2a Java I can't access that because this is a completely separate class Hopefully that will make a little bit more sense as we go on Now this class definition isn't actually gonna be really running in the sense that this is Okay, so our program as you saw in the previous video starts at main Okay, and starts running here But what we can do is we can use other classes and their attributes and their methods So what that's gonna look like Okay, so I've got a dog and so what I'm gonna be doing is I'm gonna be making quote-unquote some dogs So you'll see here. I've got dog Notice the dog is the class and dog is the name of this method there's nothing in the parentheses and This is called the class constructor So when I create a new dog this Method is called and what that method is gonna do is it's going to say I am a newborn puppy and It will create a name and an age which will be blank and Zero for that particular puppy or for that dog Now I've also got another Method constructor actually in this case constructor method called dog and in it. It's got string and my name Okay, so if we what we can do is if we want to when we create the dog We can give it a name Okay, so now we're not gonna worry so much about how that works all we're gonna look at for this particular unit is looking at the signature which is the name and then the types of Parameters that are being sent and in this case no parameters in this case a string and Then we'll look at some other methods and how to call them So in this unit we're learning how to use objects. We're not really learning how to make objects So I'll be skipping over some information in a later unit We'll talk about how to write our own objects and this is the order that the college board has suggested So we'll try it their way So I want to go back to my unit to a and what I want to do in here is I want to Actually construct an object so 2.2 which is constructors parameters and Overloading and I kind of hinted at this a little bit earlier So what I can do is I can make a new dog and notice this dog is the same as this file name So which is the same as the class So I'm making I'm saying that dog lucky. That was my dog's name lucky Equals a new dog And I'm gonna put lucky in there So I'm gonna run this and see what happens So you see on the screen. I am a newborn puppy. My name is lucky So what we've done is we've created a new dog called lucky He is new and we said his name is lucky. So if I go back to my dog class Now notice here, there's nothing in the parentheses. So it did not call this constructor Rather it called this constructor because the signatures match. So string my name And then down here String so I this is a string so it found this constructor So you'll see I am a newborn puppy. It made its name equal to my name, which is what we sent to it And it says my name is Lucky in this case. Okay, so I could make another dog. I could say Say my mom's dog say dog Bella equals new dog Bella And in that case what we'll end up having is two different dogs So what we've done here is we've constructed two new dogs One has the name lucky one has a name Bella Okay, so we've sent as a parameter To the constructor a string So we look there's no string here, but there's a string here and it call it prints this out Sets that instance's name to whatever we sent to it, which is my name. So lucky lucky So name equals lucky and then it prints out my name is name or in this case Or in the case of Bella it prints out Bella. So once we've done that We've got two different instances of dog, so the dog And I know this is confusing, but it'll come to you as we practice so Each of these dogs is different makes sense They have different names and right now both of them their ages are set to zero So we'll get to that in a little bit. So the next thing that we want to do is Part 2.3 is something called calling a void method A void method is a method that does not return a value So methods can either You know you can send a value to a method or you can not send a value to a method You can return a value from a method or not return a value from a method So a method that does not return a value is called void void means empty or nothing this So we're gonna be returning nothing So I've created a void method for us to use and I'm gonna use lucky Dot bark that's called bark, and I'm gonna do Bella dot bark No, for the parentheses Okay, so we run that and we'll see what happens and I'll explain it Okay, so you see lucky barks bark Bella barks bark. So if I go over to my dog class I Go down a little bit Okay, I've got a void method there says public void bark It's is void because we're not returning a value And if you're not sure what that means we'll get to that another time So system dot out that print Lynn print line name barks. So how do we know which name it is? So you see lucky what's lucky's name? Lucky, which is what we sent over here Okay, what's Bella's name? It's Bella So based on the object that we're actually calling the method on so it's object name dot method It calls that method for us here and it prints out that particular Instances name so lucky's name and Bella's name And so that is a void method So it is void because it doesn't return anything Okay, and that's yeah, it just does something doesn't return a value to our Program, okay, so part 2.4 Again, I know some of us won't make sense yet, but Don't make a little more sense. I think later calling a void method with parameters So I have another method called lucky dot set name And let's say lucky has a last name lucky Luciano. That was actually my dog's name So void means we're not returning any value But in this case you can see how we are actually sending a value in this case. It's called a parameter so now if I go over to Set name You'll see it's void because I'm not returning anything I'm setting the name and you'll see this a lot in Java set and some type of variable and Then get we'll get to that in a little bit. So I'm sending a string and it's called my name And so what I do is I'm setting the name So lucky's name change it from Whatever it was to whatever parameter I sent so over here. I'm sending lucky Luciano So my name is lucky Luciano, so then the name of lucky then becomes lucky Luciano So let me run that and try it again. I set the name, but I didn't print it out. So system Dot out that print ln now. Here's something interesting So my name is Okay, now see what happens here. Okay, so we have an error Says name has private access in dog Okay, so this is where the public and private thing comes in Because over here We said that the name is private Okay, what that means is over here in a different class. We can't directly access it Okay, normally we could do lucky dot name or object dot attribute is the general terminology But here we can't do that because we said it was private Forever reason on the AP test. They are looking for The use of private so when you are writing your tests make sure that you use private Okay, because you will lose a point for that or as I am told so what we need to do is we're going to be Calling a non void method Okay, so Okay, so calling a non void method is a little bit different Do that so if I look over here my next method on my list Okay is get name so Non void so he this is a void method. So you see it void here. This is a non void method So I'm going to be returning a string Okay, so this is a very simple thing. So in this case I've got get name and I'm returning name now up here Name is a string so they have to match So if I come back down I'm going to be returning a name And that is a string. So everything has to match up. So that's how I'm going to be able to get the name out of my Object so what I'm going to do here is I'm going to say system.out.println My name is Quote plus lucky dot get name now. It's a method. So I need the parentheses So the reason I can do get name Is because over here it is public Public means from a different class. I can access it The reason I couldn't do the other one which is lucky dot name. It's because it was private Now I could have made this public. It is possible. It's not impossible I could have actually made this private Okay, there there's no rules on what you have to do as I mentioned earlier with the AP test They like to see private when you write your classes So then you'll use set and get to change the values There are some other advantages doing it that way, but for now, let's just you know keep it at that So I'm going to try that and run it and see what happens here Okay, so you can see here lucky barks. My name is lucky Luciano. So then now if I do Lucky dot oops lucky dot bark. Let's see what happens Okay, so because I change the name from lucky to lucky Luciano We see which lucky Luciano barks Okay, so that's that is how that works And then there's one last thing I just want to point out real quick the null keyword Okay, so And this is something that beginners especially have trouble with So for example No, just basically means that it doesn't exist at all. So if I say like this if I say Lucky dot set name Like that. I say lucky Now you wouldn't do this doesn't make any sense, but just to demonstrate the principle if I run that Okay, so I change the name and now there's nothing there. Okay, that is not null. Okay, there is still a Value there. It's empty, but there is still a value. Okay, what null means and what when you see null Is when you see you can just go something like say lucky equals null And what that does is it completely deletes that object from the computer's memory It is no longer an active object. Okay, the garbage collector Which don't need to worry about we'll come along and free up that memory space for you Okay, because it is now null. So if I do this lucky dot bark We're gonna see some type of error So you say exception. Okay in Java when you see exception that means an error In thread main Java. So we were inside of the main method It was a Java language null pointer exception Okay, so I try to call the bark method on Lucky which is no longer a dog. Lucky is null. It has no meaning. It has no value There's nothing associated with it. Okay, so this is called a null pointer exception Okay So that's something that you will see from time to time when you're done using an object You can set it to null and what that will do is it will free up the memory So you don't run out of memory on your computer again for the programs You'll be writing probably won't matter, but it's just something to be aware of Okay, so just a real quick Wrap up here unit 2a. We use constructors to make a new instance of an object The class dog in this case defines What this object? Like what attributes it has and what methods and what it can do So this class defines The the variables the instance variables it differ and the constructor So depending on whether or not you send the right types of parameters and the right number of parameters it will call different constructors or methods and this is called overloading same name But it has a different signature. Okay, so based on the signature the system will choose the right constructor or method in this case Um, there are void methods which do not return a value. So you have to put void in here Um, there are methods that do not receive a parameter There are methods that do receive parameters and we could end up we could have done also into my age And then we could have done something like age equals my age So we could have done it like that. We could have made a new method That has all of that. It's really up to the programmer and the design and the requirements of that problem and then there's also Calling non void methods and these return a value So you have the return keyword and you have to tell what type of value it's going to be returning So this value type has to match the type of that or that variable And then over here is where we actually did all those things. We called the method So here we define the methods and the attributes Public and private access and over here We actually call them and actually use them. Okay, so you see there's a separation Of what the class is and the actual instances that we're working with Okay, so I'm going to stop there for this part unit 2a or and unit 2b We'll see this in a bit maybe a bit more practical sense with the string class Okay, so stay tuned