Some Good Java OOP Features
Java Object Oriented Programming Core – Part 2
Forward: In this part of the series, I explain some good Java OOP features.
By: Chrysanthus Date Published: 3 Sep 2012
Introduction
Reading the Value of a Property
In the previous part of the series, the values of object properties were read, but that is not the conventional way. If you want your Java program to read the value of a property, it is conventional to write a method called, get. Java has not imposed this name, get, for you; it is just conventional. So you have to write the name and the method body (block) on your own accord. The method call is usually like this:
getProperty()
taking no argument.
Consider the following class, where the getNum2() method reads (gets) the value of the property, num2:
class Calculator
{
int num1;
int num2;
int add()
{
int sum = num1 + num2;
return sum;
}
int getNum2()
{
return num2;
}
}
The class has two properties, num1 and num2. The class has two methods, add() and getNum2(). Our focus here is in the getNum2() method. Inside the method, you have “return num2;” In this class, the num2 property (variable) can be seen in the block of the getNum2() method. So, whatever value is assigned to the property in the instantiated object, the block of the “getNum2()” will see it, in the object. The return statement in the block sees the num2 property and returns its value. Once a property (or variable) is seen, its value is automatically seen as well.
In the following program, the class, Calculator is instantiated. A value of 5 is assigned to the property, num2 of the instantiated object. This value is read (gotten) by the method call, “getNum2()”. Read and try the code:
class Calculator
{
int num1;
int num2;
int add()
{
int sum = num1 + num2;
return sum;
}
int getNum2()
{
return num2;
}
}
class Getting
{
public static void main(String[] args)
{
Calculator obj = new Calculator();
obj.num2 = 5;
int result = obj.getNum2();
System.out.println(result);
}
}
A property can be declared without any value assigned to it. Also, an assigned value to a property can be changed. If you give value to a property for the first time, or if you change the value, you are said to be setting the property. The phrase “to set a property” is a convention. Java has not imposed that to us. We just use it and code it conventionally. Setting a value for a property is like the opposite of getting the value. You just need another method description in the class, to do this. Conventionally the method call writing is,
setProperty()
In the above code, the value for num2 is assigned to a property of the instantiated object. Remember, an instantiated object has all the properties and methods of the class. Conventionally, a better way to say or do this "is to set the value", with a setProperty() method. In the following code, the value of the num2 property is set (instead of just being assigned), in the instantiated object, by the setNum2() method.
class Calculator
{
int num1;
int num2;
int add()
{
int sum = num1 + num2;
return sum;
}
void setNum2(int no)
{
num2 = no;
}
int getNum2()
{
return num2;
}
}
class Setting
{
public static void main(String[] args)
{
Calculator obj = new Calculator();
obj.setNum2(6);
int result = obj.getNum2();
System.out.println(result);
}
}
In the Calculator class, the “setNum2(int no)” method has a parameter. The value of the parameter is the replacement value to the property. The block of the method can see the num2 variable. The return type of the method is void, meaning the method returns nothing and has no return statement.
In the main method, the class is instantiated to an object called, obj. The method call, setNum2(), with an argument of 6 is used to give the num2 property of the instantiated object, the value of 6. The next statement in the main method gets this value from the instantiated object and the last statement displays it.
Read the above code again and try it.
It is not only primitive objects that can be assigned to property variables of a class. Instantiated objects can also be assigned. In this case, the data type is the class name. The following program illustrates this:
class Sampl
{
boolean bool = false;
}
class Illustration
{
Sampl prop;
}
class InstantProp
{
public static void main(String[] args)
{
Illustration obj = new Illustration();
Sampl sam = new Sampl();
obj.prop = sam;
Boolean boo = obj.prop.bool;
System.out.println(boo);
}
}
There are three classes in the program. You have the main class, and then the Illustration class and then the Sampl class. This Illustration class has just one property that is not initialized. The data type of this property is the name of the first class. What you have for the property is just declaration and no assignment (no initialization). The Sampl class also has one property, but it is initialized, and the property is of a primitive data type.
In the main method, the first statement instantiates the Illustration class into the object, obj. The second statement instantiates the Sampl class into the object, sam. The third statement assigns the sam object to the property, prop, of the instantiated object, obj.
The fourth statement in main reads the value of the property in the object, obj and assigns it to a Boolean data type. The right operand of the assignment operator of the fourth statement is:
obj.prop.bool
Note the use of the three dots. The first dot accesses the property of the instantiated object, obj. The second dot accesses the property of the object, sam, which happens to be the value of the property of the object, obj. Remember, to access any property of any instantiated object, you use the dot; it does not matter whether the property is also an instantiated object. That is why you have the two dots.
The last statement of main displays the value assigned to the property of sam, which happens to be a property of obj.
An array is also a kind of instantiated object. So in Java, if an instantiated object can be a property of another instantiated object, an array can also be a property of an instantiated object. The following program illustrates this:
class Calculator
{
int[] arr = new int[3];
int add()
{
int sum = arr[0] + arr[1] + arr[2];
return sum;
}
}
class ArrayProp
{
public static void main(String[] args)
{
Calculator obj = new Calculator();
obj.arr[0] = 3;
obj.arr[1] = 4;
obj.arr[2] = 5;
int result = obj.add();
System.out.println(result);
}
}
Read and try the code. In this case, you can instantiate the array as property in the class (description), as in:
int[] arr = new int[3];
You can replace this statement with two statements but not in the class description. The declaration part will be in the class description and the instantiation part will be in main applied to an object. I leave the illustration of that as an exercise for you.
Remember, an instantiated object adopts all the properties and methods of the class. So, note how the values of the array of the instantiated object have been assigned in main. Also note how the add() method adds the values of the array in the class description. The add() method can see the array property and its arrays. It is the instantiated object that operates; not the class. So the addition statement in the method description, as it has no values yet, simply indicates that addition will take place; it is not the actual addition. The actual addition takes place in the instantiated object.
String as Property
Hey, you can also have a string as property for a class and ultimately its instantiated objects. Remember, a usable string is also an instantiated object. In the following example, the string as property is initialized in the class. You can also initialize the string for a particular instantiated object in main – I leave that as an exercise for you.
class Illustration
{
String str = "We are the world. We are the children. etc.";
}
class StringProp
{
public static void main(String[] args)
{
Illustration obj = new Illustration();
System.out.println(obj.str);
}
}
Read and try the code. Note: if you assign a string literal for the first time to an object, the literal is only for that object. For the above case, the literal is for any instantiated object.
That is it for this part of the series. I hope you are beginning to appreciate how you can manipulate Java objects. We continue in the next part of the series.
Chrys