We write codes that create Java objects from Java classes. These can have properties and behaviors. Properties are data with names; while, behaviors are the actions an object can perform on its properties. Consider the following Java class without properties and behaviors.
1 2 | public class ToyotaFortunerBlueprint { } |
A Java class without properties and behaviors is generally useless. If that Java class is to have properties and behaviors, we will rewrite it as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | public class ToyotaFortunerBlueprint { // Properties private String make = "Toyota"; private String model = "Fortuner"; private String year = "2018"; private String country = "Philippines"; // Behaviors public String getMake() { return make; } public void setMake(String make) { this.make = make; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } } |
The class now has four properties and eight behaviors.
Java Object Properties Are Named Data
Java objects can have properties and behaviors. Properties are data with names because we can refer to them using variables. For instance, we refer to data “Toyota,” “Fortuner,” “2018”, and “Philippines” using make, model, year, and country, respectively.
Technically, these names are variables in the computer programming context. Specifically, we have instance variables. We’ll dive into more technical stuff in later posts.
Behaviors Are Actions On Data
Behaviors are actions a Java object can perform on its data. To design behaviors in a class, we write methods. Consider the following methods from our modified class.
How To Use The New Class
Consider the following startup class that uses our class that now has properties and behaviors.
1 2 3 4 5 6 7 8 9 10 11 12 | public class ToyotaCarApplication { public static void main(String[] args) { // Mother's actual Toyota Fortuner car ToyotaFortunerBlueprint motherToyotaCar = new ToyotaFortunerBlueprint(); System.out.println(motherToyotaCar.getCountry()); System.out.println(motherToyotaCar.getMake()); System.out.println(motherToyotaCar.getModel()); System.out.println(motherToyotaCar.getYear()); } } |
When we run these codes, we get the following output.
1 2 3 4 | Philippines Toyota Fortuner 2018 |
The codes create a “default” car on line 8. We call it default because it already has data for its make, model, year, and country. We use four methods to retrieve the car’s data by calling them on the Java object itself.
For example, to get the car’s make, we’d use the following code.
1 | motherToyotaCar.getMake(); |
We can change the data of the Java object using its behaviors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class ToyotaCarApplication { public static void main(String[] args) { // Mother's actual Toyota Fortuner car ToyotaFortunerBlueprint motherToyotaCar = new ToyotaFortunerBlueprint(); motherToyotaCar.setMake("Mazda"); motherToyotaCar.setModel("Mazda6"); motherToyotaCar.setYear("2014"); motherToyotaCar.setCountry("Philippines"); System.out.println(motherToyotaCar.getCountry()); System.out.println(motherToyotaCar.getMake()); System.out.println(motherToyotaCar.getModel()); System.out.println(motherToyotaCar.getYear()); } } |
The codes generate the following output.
1 2 3 4 | Philippines Mazda Mazda6 2014 |
Although we started with a Toyota Fortuner, we changed the car’s properties using its behaviors. Then, when we retrieved the make again and got a different value. Now, the car is a Mazda6.
What’s Next?
There is more to Java classes than meet the eyes. Next, we’ll go through the access modifiers for classes.