OOP: Classes & Objects

Learn the Basic Java Concepts Object Oriented Programming
12 minutes
Share the link to this page
Copied
  Completed

Transcript

Hello there, and welcome back to this Java development course. So last time we went over recursion and got some homework to do, your job was to create a recursive method, which will print all the numbers from one to 100 excluding odd numbers. So here's essentially what should have done. So here I have a static void print numbers which takes in an integer a as input, then if a percent two is equal to zero, so if a is divisible by two, then we print a, otherwise we don't. And if a is less than 100, we, we you know, run the next method, and then pass in a plus one. So in result, here's what we get, we get 246 810, and so on until 100.

So we're gonna actually just start the method in Maine, passing in a one that actually works. So that is essentially we should have done so recursion is actually a pretty important part of programming in general. Essentially, there's two ways of doing things, you can either use a loop or recursion, you know, sometimes recursion better, recursion is better. Sometimes a loop is better really depends on what you do. So for example, there's this thing called a binary tree, which actually is a very important thing. It actually can, you know, speed up how computers work with data significantly, like a lot.

So, essentially, short search. And usually the way we would work with a binary tree is by using recursion. So it's a pretty, pretty important thing to understand, especially if you're going to be going over into the professional level, working with a lot of data. So yeah, anyway, that's, that's essentially what should have done and yeah, without further ado, then let's get into today's lesson. Alright, so today we're going to be going over classes and objects. So this is essentially the first lesson in object oriented programming, which is sort of a different story altogether, sort of so before we've just finished essentially doing linear programming sort of, now we're going to be doing object oriented programming.

So this is a whole nother beast, sort of that we're tackling. So to do that, let's first of all create a new package, since our old package is getting kind of, kind of, kind of messy. There's a lot of things in there. So yeah, what's your new package? We're going to call this package o p, which stands for object oriented programming. I mean, package named user.

Okay, let's actually let's actually call it object oriented programming, just to abide with convention. All right, there we go. Inside here, we're going to create a new class, classes and objects. Let's add a public static void Main method and finish. Alright, here we go classes and objects. So what is a class Well, everybody We've just created the class.

So, right here in Project one, all of this is a class. So if you'll see if we actually open up something, let's say RF medic operators, you'll see we have public class Earth medic operators. So what we've created is a class. There's a close out here. Yeah, save all of them. All right, there we go.

So inside of classes and objects, so this is also a class we have a public class classes and objects. So before it may seem a little bit confusing as to what exactly we were doing, since essentially, we were creating the classes, but they weren't really classes where they so we have our medic operators arrays, isn't really a class. So to give you a better idea of exactly what a class is, we can now do new class. And let's call this class. Let's say zebra, okay? zebra.

There we go. We don't need a main method, your zebra, okay. So this class zebra will have two different attributes so the zebra will have weight It's gonna be an int, no double, double weight and double height. There we go. So our zebra has a weight and height. Now zebra is a class that has two attributes, weight, and height.

Now, what is an object well inside our classes and objects, whoops, what we can do is we can do zebra, and it's going to be the zebras name is going to be Bob equals, and the new zebra. Now remember how when we create a scanner, we do scanner s equals new scanner system.in inside the parentheses, well, this is the same thing, but instead of doing new scanner, we'll do new zebra. And we don't actually pass anything in that's called the constructor. And that's and we'll go over that in in one of the next lessons. So yeah, For now just leave the parentheses blank. But you don't need them.

If you don't have them. They don't give you an error. So yeah, so you do need the parentheses. So zebra Bob equals new zebra This will create a new object of type zebra. So this is an object right now Bob is an object. So now we can actually do Bob dot height is equal to 4.5.

And then we can do System dot out dot print ln Bob dot height. All right, there we go. Whoops, what's what what what what? What happened? Okay, this is very odd. There we go.

That's, that's weird. All right, let's run this. There we go. And as you can see, since you said Bob, since we set Bob dot height to 4.5. Now, we when we print Bob dot height, it gets 4.5. We can do the same exact thing with weight.

So Bob dot weight is equal to 5.7. Now if we do System dot out dot print ln Bob dot weight will now get 5.7 There we go. All right. Now what else can we do? Well, inside our zebra class, we can also create methods. So this right here is actually called an attribute.

So this is just a data type inside our zebra class. That said, we can also create a method inside a class so we can do public. void, no public string. Get the bra name once he gets the Bernie okay. So this is a method that will return a return statement. Anyway, this is a method that will return a string of the zebras name.

So now we can do return something I don't miss a bob. Okay. All right, there we go. So now inside our zebra class, we have a method get zebra name, which will return Bob. So if we go ahead and actually do it now we can now do System dot out dot print ln Bob dot get zebra name. And now if we run this, we get Bob.

Since right here, we have a method that returns Bob. So this is essentially the very simple part of object oriented programming. It's the very basics. So a couple things to understand a couple things to understand. So first of all, weight and height, in this case, are variables that are created inside of the class. So before, we've created variables inside methods, so inside the main method, we can also have variables being created inside the class now within a method, so we could have a zebra Bob here as well.

Zebra Bob, as you can see, and so in this case, zebra Bob is now just In an attribute of the class classes and objects, we can even instantiate them as well. So we can use zebra Bob equals new zebra. There we go. But what we can do is operation so if we do Bob dot set bob.we can't do this. So you see, we can't access Bob's anything. So if we do Bob dot weight equals 4.5, you'll see that it actually gives us an error, since we would have to do an operation on bob inside a method.

Now at this point, I'm sure you're wondering, so what exactly is going on? So we have a bug here, and we have a bug here, as you can see, and it's not giving us any error. So if we remove this, we actually everything's okay. Well, there's a couple things to understand. So first of all, right here we have a static void and so in result, you can't access a static, non static attribute Bob from a static method. So to those add static here, and there we go.

So now you actually can access it. So you can do Bob dot height equals 12. There we go, and it will refer to this Bob here. But if we put Bob dot height here, then we refer to this Bob right here. So you'll see how how this sort of works. So since this was created a method and since this was creating a class, we can essentially have the same name.

And if if there is no zebra Bob, then it's gonna refer to the static the the attribute Bob inside the class. If there is a zebra Bob inside the method, then it will refer to the bob that's inside the method. Now, I'm sure at this point, you're wondering what if we already have Bob? Sort of what if we already have Bob created Good, right? So, but we want to refer to this ball while we would just have to do this.so. This is essentially just a key word that refers to the class.

So it says that in the inside this class, we're going to refer to Bob. Now, we can actually use this right here because we have a static method. And again, we're going to go over static methods soon. So there's going to be there's, there's actually a whole different lesson that we're going to go over called modifier types, which, you know, public static. Let's see what else is there, public static private diff, well yet, I guess, default package, and so on, and so on. So there's a lot of different modifier types that we can go over, that essentially sort of can create different change attributes and methods.

So in this case, we actually can't use this if you hover over it cannot use this in a static context. But yeah, you get you get the idea. So here, it's gonna refer to Bob since Bob's already created inside the method, if it isn't created, it's gonna refer to Up here, and if we change the name, then everything with Bob one is going to refer to this one here. Okay, so that is the very basics of classes and objects for homework. Your job is to create a one other animal I Hippo class with two attributes, so it's going to have the weight of the hippo in grams, and then the height of the hippo in millimeters, okay? And then you're going to have two methods as well, which will return one of which will return the weight, not in grams, but in kilograms.

And then the other will return the height in meters and not millimeters. Okay. So there we go. That's your homework. Pretty simple, not too difficult, and you should get it pretty simply. This is just an introduction to classes and objects later on.

In the next lesson, actually, we're going to be going a little bit deeper into encapsulation and what that means. So yeah. Anyway, I wish you luck with your homework and I'll see you next time.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.