Welcome to another tutorial for the "How to code in Java" tutorial series. In the last tutorial, we have gone through on the basic concept of object-oriented programming. In this tutorial, we'll be diving deeper into classes and object by demonstrating how to use classes and objects in Java. Without further ado, let's get started.
Index for this tutorial:
- Creating a new class.
- Class's methods and attributes.
- Creating an Object or Instance.
- Using multiple objects.
- Passing an object as a method's arguments.
- Full code.
Creating a new class.
I'll be using back the example from the previous tutorial where I used a dog as an example for a class. To define/create a class in Java, we need to use the keyword "class".
Declaring a new class. |
Class's methods and attributes.
As I mentioned in the previous tutorial, a class is constructed with two things; methods and attributes/fields. Now let's add in some attributes and methods into the class. Let's create a method for the dog to bark and some attributes to store the dog's breed, name, age and gender.
Class's methods and attributes. |
You can read this tutorial to learn how to define a method. While to define an attribute for a class, you'll need to first decide a few things:
- Which access modifier to use.
- Data-type of the attribute.
- Name of the attribute.
- The initial value (optional).
The structure of attributes. |
Another thing is that the fields/attributes inside a class are actually variables. Variable is a component which lets us store information on runtime (during the execution of the program. When a variable is defined in a class, we need to define a new object/instance to use or change its value. But that's not the case for static attributes/fields. For a brief explanation on access modifier, please refer to the previous tutorial.
The attributes define the state of the object. For example, the class "Dog" has an attribute "breed", which indicate which breed does the dog object belongs to. This might be different for other objects.
While methods (functions that's inside a class) define the possible behaviour of an object. For example, the "bark" method in the "Dog" class defines that a "Dog" object could perform the action "bark", which prints out "arf woof woof woof". We could also add some other behaviour and attributes for the "Dog" class.
Some examples are adding an attribute to keep track of their position to see whether they're sitting, crouching, or laying down. As well as the action for changing between the position using methods.
Full code for the Dog class. |
Creating an Object or Instance.
Now, after we have defined the methods and attributes that we need, we can create as many objects that we want. We won't be bothering with the other fields except for the "position" in this tutorial as the rest will be used in the next tutorial, where we'll see how we can use a constructor to set up the initial value for our class's attributes. For now, let's just focus on how the basic things work.
To create a new object, we just need to use the name of the class that we want to use as the variable's data type. When we used a class name as a variable's data type, the variable will store the reference to the object's memory location. Now, let's create a new object while using "obj" as the object's name in our main method. Then, initialize it with a "Dog"' class object.
Creating a new object named dog for the "Dog" class |
To call a method from an object, we just need to use the dot(.) notation followed by the method call.
Calling a method from the object. |
Result for calling the method "bark()". |
Now, let's make the dog sit, then crouch then lay down.
Calling the remaining methods. |
Result of the methods call. |
Besides calling a method, we can also access the attributes of an object. Which works just like using a variable's value.
Accessing object's attributes by using the dot (.) notation. |
Result for the code just now. |
Using multiple objects.
Result for the code above. |
As you can see, although we changed the value of the position for the object "dog2", the value that's inside "dog" object didn't change. This proves that all object are independent of others object, where changes in one object will not affect the others object.
Passing an object as a method's arguments.
Passing an object into a method where the object value will be changed. |
The example class for the object passing demonstration. |
Full code
Well, that's about all the basic things you need to know to work with classes and objects. If you find this tutorial helpful, please share it with others that might need it or are interested in it. Also, if you have any questions, suggestions or critics, feel free to post a comment at the comment section below. In the next tutorial, we'll be going through on how to use the constructor in Java.
No comments:
Post a Comment