Transcript for:
Java Object-Oriented Programming Overview

Hey, good morning everybody. Welcome to your course on Java object-oriented programming. Before we get started, I wanted to mention that we're not going to start from the complete beginning of Java, so if you need any additional help, well, there's some resources out there. One, I have a Java series on YouTube, and I have this course on O'Reilly object-oriented programming with Java. And I'm not sharing this because I make a ton of money. No, unfortunately. I'm sharing this because it's a great resource, and I think you can get started with a free trial and just watch it. So, This is going to go in a ton more detail than what we're going to talk about in these videos, because these videos are really just designed to get to the point and really not go into a terrible amount of detail. I mean, it's not going to be nothing, but at the same time, I'm not going to overwhelm you with a bunch of stuff. So what we're going to do is we're just going to learn the basics of getting started with object oriented programming. So I'm already really assuming you know how to make a project and whatever editor you're using. It doesn't really matter whatever editor you're using. as long as you can run the Java program. However, if you're using IntelliJ, then boy do I have a surprise for you, and that is our sponsor for this series. This video is sponsored by DiffBlue. DiffBlue offers a free AI-powered unit test generation tool for Java developers. DiffBlue writes your unit tests for you and delivers human-readable code to increase your test coverage and speed up your development, while ensuring you didn't break anything along the way. With a free community edition available as an IntelliJ plugin, DiffBlue is super easy to get started with. Best of all, as a viewer of my channel, you can get a free license upgrade to automatically write tests for either open source or commercial code. Get started using the link below. So sponsors are how I actually make money on this YouTube channel, so I would appreciate it if you guys check that out if you want to support the channel. So I have this project here, we will just right click new class. I assume you guys know how to create a project and like the bare basics of Java. We'll just call this OOP. And public static void main. This is where our main method is going to be. And there we go. So let's just make sure everything's working. We'll say, Claire, you're emailing me at 2 in the morning. Are you serious? So let's just try saying hello world and run this. And we get hello world. All right, so we got a working application. And we can just talk about some of the different components in here. So everything is defined within a class. And inside of this class, we have. what's known as a method. This is a static method. We might talk about that soon, but basically this method gets ran immediately when our application launches. And inside of this method, we are invoking another method, println. And this here that we're passing in is known as an argument. So anytime we're passing in data, it's an argument. A parameter, although these terms are often used interchangeably, is slightly different. And that is when you define a method, you can define a variable. to take this information. So someone wrote this print line method to take one parameter. And we might be able to right click and go to definition or something. So open declaration. And here is the definition for it. So it takes one string called x. And they refer to that string throughout this method by the name x. Let's back up just a little bit and talk a little bit about this out here. So we can right click. Open declaration and this is a static variable again that static word comes up and basically Static means you don't have to create an instance of the class So we'll talk about that when we create our own class, but that is just the basics so to see how that works We're going to create our own class now, so let's go ahead and X out of these Documents there and what we're going to do is we're going to basically talk about how to create an instance of a class Now when you create an instance of a class, it's known as an object. And if you're new to this, you know, I'm throwing out a ton of words. Anytime you hear a new word, I want you to write it down because you're going to need a vocabulary list by the end of this series if you're new. So we're going to create an object, which is an instance of a class. And here's how that's going to look. You say what type it is. And the whole point of object-oriented programming is so that we can create custom types. So a user is very specific. It's not like a string or an integer. It's something specific to our application. You know, we're going to create an application to store users. So we say what type it is, then we give that variable a name. This is no different than if we did something like int x. Instead of saying int, we're saying user, and instead of saying x, we're saying you. Or, you know, you could put Caleb or something, but we'll just go with a general. name here such as you and then the way you create the object is you say new and then the type again user and then parentheses. So we're going to get some errors and I'm going to talk about how to fix this in a second but anytime You do this, you're creating an object, and that object is stored in this variable u. Now, when you say new behind something that looks like a method, well, this is known as a constructor. We're invoking the default constructor for this user class. Again, a lot of words. We'll understand all those by the end of this. So when we do this, this is going to give us a new user object, and that's what we're assigning to u. So now we need to create this user class so we can hover over user and say create class user. If you're not using Eclipse, it might be a little bit different, but you can also just manually create the file as long as everything matches up. So everything looks good. We just hit finish and we have this class user. And what that's going to do is it's automatically going to get rid of that error. So we should be able to save and we should be able to run. Now our application doesn't do a whole lot more. However, it's working. So that's progress, right? And now that we got pretty much our structure we're going to be using, I'm going to be minimizing this package explorer. All right, so we created a user. That's it. That's how you instantiate a class into an object. We're done. But honestly, this isn't really that useful. Like, what do we do with this user? Well, that's where attributes come in. You know, we can associate different variables with the objects. So let's go into our class. And here's where we define the things we want to store about each user. So what we do is we just give it a type and then a name, just like creating any other variable. So we'll go ahead and say string, and we'll give this person a name, and then we'll say string. And let's say this is for a website. You know, maybe you have a subscription. We'll say string membership. And now what we can do is we can assign values to these two variables. So I'll do that over in our main method. So we'll say you dot and you can see these pop up right here. So it's a string membership and a string name So let's give this person a name and we'll set that equal to Caleb There we go. And then we'll say you dot and this time we'll go with membership and obviously guys I'm gonna have the gold membership and there we go. So at this point you might be wondering like, is this really that helpful? Do we need to create this user object and just to create variables? Well, right now, it doesn't seem that useful. But as we get more and more stuff describing a user, well, it's nice to just keep things organized. And this basically allows us to create code that matches the domain requirements or the business requirements of the application more specifically. So the code is much more friendly when we are developing instead of just having a bunch of variables thrown out everywhere or a bunch of arrays we can make it specific to the problems we're trying to solve so objects basically describe real-world things and it's so much more helpful it's a better way to design our software now there's a lot of different other ways to design software and a lot of different arguments so I'm not saying this is the only way to do it but it is important to know now the benefit here is since we created a user class we can basically use it as a template And that's what I want you to think of the class. I want you to think of it as a cookie cutter that you can use to make a bunch of cookies. You don't actually really use the class itself very often unless you have static stuff in there, which again like static you don't have to do this part of it. You don't have to instantiate here, but you can think of that cookie cutter as something if you're making cookies you know you're not going to eat that cookie cutter. You're going to use it to create new cookies that you're going to then eat. So the same thing applies in programming. You're not going to be using that cookie cutter a whole lot. You're not going to be using that class a whole lot. But more than likely, you'll be creating objects and using those objects. And the great thing here is that we can use that user class to create multiple users. So for example, we could say user U2 and create a new user. And for this one, we can give a different name. And we can give it a different membership. There we go. So how do we actually access these values? Well let's just go ahead and print something. So we will say u.name and then we'll do the same thing for the other one. So we'll say system and this one will get u2.name. So that is how you access those elements. You use the dot operator on the object. So when we run this we should get the hello world that we created up here. And then we get Sally and Nall. And you can see I made a really dumb mistake because this is why I probably shouldn't make videos at 2.30 a.m. But no more mistakes from this point on. But those things happen. So let's try this again. We'll run this. And now we got Caleb and Sally. So that's another important thing when we are working with objects. If you don't assign a value to those attributes, they are null. Unless, of course, you're using something such as an integer. Let me just go through a really simple example just to show if we had the age of the person here and let's just say we printed U dot age that will default to zero so strings and any reference types like other objects are going to be null The primitives are going to have a default value such as zero So if you get much into programming you're gonna hear a lot about getters and setters and a concept known as encapsulation Encapsulation is one of the big three pillars of object-oriented programming. Now before we start a war in the comments section, although if you want to do that, it's fine because you know that increases my engagement and stuff, but there's argument about how many pillars there are, but there's three really main ones that I see. Encapsulation, inheritance, and polymorphism.