9 October 2020

Classes and Objects in Java.

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.


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.



For starter, we won't use any access modifier. That means to leave it blank. By doing that means we're specifying the default access modifier to the class, methods, or attributes/fields. The default access modifier specifies that the class, method or attribute could only be accessible within the same package (folder).


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.


As you can see, we can access the attributes by using the dot (.) notation. But, if we didn't initialise or provide the attributes with any values, we'll get back null, 0, etc. depending on the data type of the variable.


Using multiple objects.


Now, as I mentioned above, a class could be used to create as many objects as you want. To create another object for the "Dog" class, we just need to reference a new variable to a new object.

Creating multiple objects from the "Dog" class

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. 


In the previous tutorial where we went through on how to pass arguments value into the method's parameter. But I didn't explain how to pass an object as an argument into a parameter.  To pass an object into a method, simply use the object as the method's arguments when calling the method.

In the method parameter section, remember to use the same datatype class that you used to create the passed object just now. IF you're asking why do the changes affect the original object's field value? This has to do with the passing-by-references of Java, I'll explain about it in a future tutorial.


Passing an object into a method where the object value will be changed.


The example class for the object passing demonstration.


The result for the code.


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