Java Basics

Java Basics 12: Objects

14 videos

80 minutes

In this video we will see a very important issue, which are the objects, in any language, object-oriented this is the main chapter, so far we have seen how to store information, variables, we have also seen the Arrays and matrices, which can be of different dimensions. But nevertheless, There are times when it is necessary to simulate reality objects that have many properties, where you need to save a lot of data from a single object, and

to do things with them, that's what we will see in this video, how to transform into object codes real life. Not only have properties, not with those who can do action, imagine for example: a window, a window may have a width, a height, you may have information if open or closed, but other things can be done with the window, openable, closable, cleaned, can be broken, because all of that code, has

that can become somehow, and that are the objects. So I'll show you how we could do this, look. Here, in the part of Netbeans, we had closed this section of the show you here, the tree is where all file structure, which make our project. Well, in this package, which is where we are programming so far, here you see that we are already in the package, in this package, we click the right mouse button, and

say: 'New Java Class' a new class, in this case so far, we've been doing the example of a train, then the name of the class will say that is: train, ok! We, Finish, and here we will create a new file inside, here you see, 'the tren.java' that has opened me here aside, this tren.java, we see that here puts, pubic class. Well here within we will create the characteristics of the trains, ok! So how do I here to

link it to this point java, this file, link it here so I can create me train here, well let's import, we will put a import, put the package name, cursobásicojotajotavm., and by giving the point, to see not know if I wrote right or wrong ... and give the point, here I get the files, these files, these files are in the package, then I, this file I'm working you want to import the tren.java, because I select

the train and I was already imported, ok! Now I already can use, well I here tells me that I have not used so far, and I'm importing a thing I have not used, but this message will be removed, when it begins to rub shoulders, then what do I do with all this code I have here? Well all I'm going to erase this, because it was only for the explanations that had until now, that if I'm going to leave, because I want now for one thing, not now

think what I'm doing ... you will see .... this ... it took me, ok! I'll take it and put it here, well and truly so far do not worry about this, this in fact I'm going to cut, what do we do with this? Well, so that you see here in this class, class train, the first thing we have to do, ie what features will have the class, so I've created the array, where we will work equally, with the number of seats and the numbers

passengers, here is the matrix, and on the other hand I can give you more information, such as: what characteristics else has a train? ... A train has a machinist, right? Let's get us one, String, where I kept the name of the driver, and we could well be adding all the features of train, for example meters long, years old, we will put here such that data, we will put here, int, let's put here

Grandfathered having the train, ok, I put it there, what does this mean? All that any train, which I believe will have will have those features, any time I here want to create a new train, all trains will have that matrix, you will have a string with the name of the driver and will have a figure with the age of the train, they will have all but this to be created when a train I think the first thing is the constructor is invoked and what is

builder? For all objects have a constructor function that it does is to give values ​​to these data, these data have no value, not here We say as it is called the engineer, nor how old he simply is the data set but has no value when a train is created that can not be empty, must have a value, for that builders do, and here put, public, train the class name, here I tell what will be the data

that they will have these variables, when I think the train, for example here, the first thing I'll do here is tell you how many seats will have, ok, then what I'll do here is say let me put this, tell him nothing to start the train is that the number of seats is for the first car, 10, the second 20, the second carriage 30, as we have done throughout the course, and the train is empty, when you have created seats but is empty, ie

I give number of seats, but passengers tell him that all the cars are empty, zero passengers, ok, nothing that would give him when the train is created, if I did not give details, so input the engineer, the engineer, let's say it is 'Anonymous' and the number of age how old train, since we know nothing, we will give zero, ok, well What does this mean? I think here now if I train, I'll create me a train, how I think

a train? When I wanted to create an integer that read, type, int, and the name of what was going to call, because here the guy is going to be train, the name will give, first, I have created well a train, now here, the screen can show values ​​that train, for example here I will put: Sytem.out.println, and I'll show you here to show me first point, and then see the data first, or example to show me the engineer, I this I keep it, and now when

here I compile, it will load the java class will invoke the constructor, because I'm here making a variable, and after all, forgiveness here I have a Error! ... .. wait! ... .. Here'll put, 'new station' that was the ruling, first, is a new train, ok, and what about me? the engineer is called anonymous, because I have created so a no data, I called the builder, and the builder, 'public train' I have given this information if I here you say, how much

old is the train, I will say it has zero, you see? Perfect! zero, but I can do to make the train, when you think the data can finds and customize how I do that? Well here I think another train builder but here I'll tell you, he will receive data, for example, will receive a name for the driver, or for example also will also receive a number of years, so I will put, years, which will make antiquity,

then what do I do with this? The driver I'll tell you, to give you the name of the driver will do the same thing I've been through it, and the years of the old will be the same thing here I gave, so I here now I can create another train, I'll put here, and train, second, = new train, and here I'm going to pass data for example I'll tell you that the driver is called Juan Palomo, and the number of years is 25, so now here, if I try

show on screen, the age of the second will no longer be zero but 25, see it, and if I here you say, show me the engineer will tell me that flame, Juan Palomo, they see Juan Palomo. Then I can create objects with standard baseline characteristics are these, putting one here within the class, putting public, and the name of the class is the constructor, you create a standard data, but can also make data entry,

I think when the input variable, the object is already initialized data with actual values ​​I want to pass them yet, this I can change, example I can here say that the first machinist, I'll change esses data, meaning that the driver is called, Luis Garcia, well I now and you change it, so I now here, if the screen show the name of the first driver of the first train, will see that it is already changing because I can

access to that data, ok, then all these things we can do with the properties of objects, ie their values ​​of their variables assigned to them, but apart, having properties, these objects we can give them, methods, ie we can do things with functions that perform these properties, for example we will create us here, a function that what you will do here is fill a wagon, and for example we put many people in

a wagon will create a function to fill it, we will put, 'public' function name it, 'CompletarVagon' and we will have to write here, what write, what car is we want to complete a shtick to tell us, the car number 5 or 8, 14, ok! This function or functions that create these Here, they can return us value, if for example you want to see, that is how many passengers there are, tell me how many passengers there in that wagon and the function you

returns the number of passengers. But as in this case does not return anything to us, we will put here for a void, because nothing is returned, this function simply complete the car, what do you do? Well, here what we will do is this, let's say that the car, the number that have happened, here we have passed, zero or four, the car in that position where we are storing the number of passengers here, let's say that the

value is: the same seat, where are saved the seats? For in the zero position of the matrix okay? And here already, since we have already put the number of passengers is equal to the seats, as we have completed it can display a message on screen, here read: System.out.println, which says: 'the wagon' and here we wagon number we've been through, wagon, but remember, always, Arrays and matrices, account number

less, so we like, +1, 'it has been completed' well, let's see if this works, now here, that we keep going to do the following, we will consult, how many passengers are in the car No. 1, we will put here, wagons, the wagon number two, the third car, which has 30 passengers, how many passengers are there? I have to say, zero. I'll delete all this ... System.out.println, this delete them, I have to say that there is zero. Okay! Now before

this, I will say that I have, I will call first to invoke the function, CompletarVagon, and I go to car number 2, which is the third car, ok! Now I give and says: 30 See? It says: the car three has been completed. Because here it evens me and here tells me thirty, and when I consult effectively has It has been completed, also here you could create a function that will be called: meter passenger, let's see now, here, I'll create me one, MeterPasajero, and I have

to say what car has to get him and how many passengers want to stick, then here what I would do, is the following, we will say here that the number of Passengers here have to be equal to what most passengers have I told you, that is the number of passengers would have increased the amount that I have been said, in the car who asked me, but before doing this, it would be good to check whether or not capacity, because maybe a wagon is already full, and I said

I want to get a passenger but is already full, then how can I do that? Well I'll ask here, I'll say yes here, the number of passengers there so far, more than I would want to get, if it exceeds the capacity, for example, imagine that this car is 30 and there are 25 and want to get 6 and outperforms 31, then I say: if it is greater than the capacity then what I'll do here is show a message that says, 'The car is already ... there is so much

Capacity, '' there are not many seats left in that car, '' there are not many seats left on the wagon and the wagon number 'here it is! Up here! ... No many free in that car, and otherwise if found seats empty seats, well, then, yes there already in, else, put this and say that that effectively increase passengers and I'll put here a little message that says: 'The reservation is completed' let's try if this works! I come

here, and once again I have filled the carriage 30, notice! The wagon three is already filled, I'll tell you want to get a passenger there in the wagon 3 MeterPasajero in the car, in the car we'll get two passengers, I have to give an error message, I have to say that is not possible, you see? Tells me therefore no seats left on the wagon three, but now if I want to get in the car number 1, ¡there yes! The book is completed. We are going to

see now how many seats are in that car I told him to put me in the car number 1, which is the car number two matrix No. 1, I says that there are two, which are those that I've gotten from them, and if I now'll put five more, now there are 5, you see? This is already running! If you see here, here we are getting into, the number of wagons, all right, as we have seen then, as we can handle data from a class here at the builders that

defects are created with standard features, or you can customize input values, namely that we want and we have also seen how work with the methods we can do to complete, we can make functions that work there or not, with the data of the objects we are creating. Well, here we have, then, our object, and as you know this can be invoked many times you want, if I put here 5 and then again

meter, 3 could call as many times as I want, I will say that there are 8 but remember that every time you launch, restarts, and therefore every time it launches, new builders are created, so when I showed before got two told me that there were two, and when he pitched five told me that there were five not add up plus 7 because they were different pitches, and each time the object is thrown reset, but here they do once they saw that accumulates, because once seen

objects, do testing, make examples. Let's see now, heritage, ok! We spent the next video.

To find new courses, subscribe. There will be no spam, promised :)

About the Author

foto de jotajotavm
José Javier Villena

Biography: Analyst-Programmer in different languages. Tutor PREMIUM platforms recognized worldwide as CodigoFacilito. Editor Cristalab articles. My YouTube Channel is sponsored by the ANAYA and LaTostadora publisher. I like to explain in detail and give several examples for the avoidance of doubt.