Let’s continue with the explanation of the main concepts of object-oriented programming with an example. In this example we are going to use the class called “Human” that we already used here. You can follow this test in codeboard and add more attributes and methods to the Human Class, practice and make it better!
Now it is your turn to practice Java for Android development.
Read the full transcript
Hi, in the Java overview you learned about classes, objects and methods.
So, here we’re going to show you an example with an object human we’re going to make the class, the object with its attributes, methods, and constructors.
So, we are here in “codeboard”. We are going to add a file whos name will be the name of the object. In this case, it will be “human”. “Human.java”.
Here you should call. You should put the inicialization of the class then put the variables, the attributes which are the characteristics of your object. In this case our object human has some attributes like gender, first name, last name, ID an identification in your country and the birthday.
So, let´s put them. The gender would be let´s call a “comment” here it would be “f” for “female” or “m” for “male”. So, we are also gonna add string “firstNames”, “lastNames”. We are going to add the “int” with “identificationnumber” and we are going to split the bithday in year, “yearBirth” “monthBirth” and “dayBirth”.
Let’s add something else, something interesting… We’re going to add another object inside here. It will be “human”. The partner. It can be new or it can have another “human”. Let’s just leave it over here for a moment and then we’re gonna add some methods some actions.
Remember that the methods are the actions that our object “human” will do. In this case, we will have “speak”, which will print. It will print out some words that you want to add and get the “full name”, get “age” or “set” or “get id”. So, let´s add the methods.
“speak” will receive “string words” and it will just print out the words that you enter. We also have set “ID”: we are putting here “void” because it won´t return a value. Remember that. We are just gonna set the “ID”. We are gonna set this value, this attribute with the value that we’re gonna receive in this method and if we put “public int” “getId” without parameters it will return “identification number” Then it will print out the value of the “Identificationnumber” variable for this human.
Let´s put the default name “Public string” “getFullName” and will return “this.firstNames” with the concatenation of a “blank space”. Let’s try. Let´s make some tests with what we have.
We’re here, we’re gonna remove the “hello world” we’re going to make a new “human Connie”. We don´t have a constructor so, we are just gonna make “new human”. turn out just to make human and to “Connie” we’re gonna “setId”, “getId” the field of “name” ok, but we don´t have a “set full name”. Let´s start with the ID separated “ID” We are gonna input an integrer. Let´s put (123456) and then Let´s see what it returns an integrator So, we are gonna print “Connie.GetId” Semicolon (;) compile and run.
So, we are here printing “connie´s Id”. If we add more methods like these ones for example “addPartner”. It will receive a human and will say that the partner which is a “human” of the current, “human” of the current object will be “human couple” which is the parameter that is receiving this method and we have to make association for both humans for both objects so we are gonna put the “human couple” which is the parameter the partner which is the value which is the attribute partner for the other human would be this, would be this, would be the current human.
You can do that. Is is just an example to show you that you can do that. We’re going to get. We have this other method the partner so “birthday” get the age, taking current date and the “birthday” that you already set. Let’s see this main method. We already have the human “Connie” with the constructor. Let´s remember a constructor helps you to create a new object we can make constructors with attributes and without attributes without parameters and the default one is without attributes and if you manipulate it , you can add it or just leave it without attributes.
We have a human called “Connie”, “Connie Smith” which is female and was born in January 22nd of 1987. If we want to print “Connie´s” full name we just call the method “getFullName” for the human “Connie”. It will also be the same for the method “getAge”.
We will make another human called “John” so we can add a partner to “Connie”. We can use Connie.addPartner method.
We’re going to add “John” which is a human and then we are gonna print out Connie´s full name and Connie´s partner name.
The same thing for “John”. Johns full name and then John´s partner name as you can see we just make one relationship but we will make the match for both. So, let´s compile it. Compilation successful and run it.
As you can see it printed Connie´s full name and age. Connie Smith is 29 years old. Then, Johns full name and John´s age. John Brown is 30 years old and then after we make the relationship of the partner we are going to print Connie Smith. Connie Smith´s partner is John Brown and Johns Brown´s partner is Connie Smith. ok