Learn more about Object oriented programming in Java

0
1220
Java (10) - OOP in Depth

Java (10) - OOP in Depth

When you create a class, you are said to encapsulate functionality inside that class. That is, any object that is instantiated from this class will have access to properties (attributes), and methods (functions). Attributes are used to describe the object. For example, a student object has a first_name, last_name, and grade attributes. Methods are used to define actions that this object is capable of. For example, our student object may have a register() method.

Declaring and calling instance fields

An object properties/ attributes are called instance fields. Each object maintains a local copy of it’s own instance fields. Multiple objects can have multiples fields of the same name, each having a different value. You declare an instance field the same way you declare a variable. For example:

As you can see, you can optionally initialize the fields before using them. If you don’t, fields will be initialized to the default value, which is 0 for numerical types (int, float, double…etc.) and an empty string for textual ones.

Accessing the instance field from inside the class definition is as simple as calling it, while doing so from outside the object requires that you prefix the field with the object name. Have a look at the following:

Notice the way we are calling the first_name instance field from inside the constructor function, and how we had to prefix it with myStudent when calling it from the main() function, outside class declaration.

In general, it is a good practice not to initialize instance fields upon creation like we did in the above example, however, to assign value to them in the constructor function of the class. For example:

In this example, the constructor function accepts 3 parametrs: _first_name, _last_name, and _age. Then, inside this function, we assigned those parameter values their respective field instances. We also have another constructor function that does not force the user to supply the student parameters on object creation, instead, they may be supplied later in the code.

Notice how we used the underscore _ character to differentiate the parameter names passed to the function from the instance field names. You can also use the keyword this to make the compiler refer to the instance field explicitly and avoid any confusion. Consider the following modified version of the above example:

Using the keyword this means that you are going to use the instance field of the class, and, thus, you can use the same names for your parameters without worrying about collisions.

Using methods

As mentioned before, methods are ways to describe a class behavior. They are created and used the same way functions are. Check out the following example:

Here we defined a method called register(). It should be called, for example, when the student pays the tuition fees to register himself/herself to a course. This method becomes immediately available to all Student objects.

A method can have a return type. In this case, it should be initialized as follows:

But in this case, you must add a return statement at the end of the method body with a matching return type, Boolean in our case. If you do not intend to return a value from the method, you must add the keyword void before it’s name in the declaration. You can also instruct it to accept parameters like the following:

It is worth noting that you can use the return statement even if the type of the method is void. In this case, the return statement will immediately terminate the function and returns control to the calling code. Take a look at this:

In this example, if the method gets called with a parameter “Information Systems”, it terminates and code execution continues past the method call. Otherwise, the method moves to the next statement.

Java Programming Course for Beginner From Scratch

Method chaining

Lots of times you’ll encounter the following form of code in Java:

Such a code is an example of method chaining. It’s a more compact way of dealing with object methods, as long as they work on the same object. To achieve this, you must declare the method return type as the object itself, and return a reference to the object. An example will make things clearer:

This is the student class. We moved it to a separate file Student.java to be able to call it without the static keyword (more on that later). Notice that the two methods register() and enroll() have the Student type. Accordingly, when they return their value, it must be of type Student. So that are returning this, which is a reference to the current object, Student. This way you can instantiate the object, and call the register() and enroll() functions on it, all in one step:

Passing an arbitrary number of objects to the method

You can pass parameters to a given method that matches the number and type of the parameters declared in it’s definition. But what if you can’t determine the exact number of parameters to pass? For example, let’s say you have a method that calculates the total of the parameters passes to it. But the number of parameters to pass is unknown to you, and it may vary from one method call to another. In this case, you can use varargs. Take a look at the following example:

The output of this code will be:

Let’s how this happened. The only change we have here is that we added three periods between the parameter type and the it’s name. This way, you can add any number of integer values to the method when calling it, and the method will sum them up for you. Notice that when a method gets called this way, the parameter name is converted to an array, on which you can iterate and do any processing needed on the values within.

Conclusion

In this article we continued our discussion of object oriented programming in Java. We examined instance fields, and instance methods. We saw how to define and access each of them. Then we discussed method chaining as a way to call object methods in a more compact way. Finally, you learned how to pass a random number of parameters to a method.

In the next articles, we delve into more aspects of methods in Java, like recursion and overloading. See you next.

LEAVE A REPLY

Please enter your comment!
Please enter your name here