Article

Object Oriented Concepts in Java - Part 1

Page: 1 2 3 4 5 6 7 Next

A Java Tree

For our first foray into object-oriented programming, I propose to implement the Tree class discussed above in Java and then write a program that uses it to instantiate a couple of Trees and make them grow a little.

Open your text editor of choice and create a new text file called Tree.java. This file will contain the definition of the Tree class, and it is important that the name of the file match the name of the class defined within it right down to the case (thus, tree.java will not do). Type the following (as usual, the line numbers are provided for your convenience only, and should not be typed as part of the code):

1  /**  
2   * Tree.java  
3   * A simple Java class.  
4   */  
5  
6  class Tree {

We begin by announcing our intention to create a class called Tree. Note again that I am observing the convention of spelling class names with a capital letter.

7    public int height = 0;  
8

Get Your Copy of Kevin Yanks Book NOW!Aside from the word public at the start of this line, this looks just like a standard variable declaration. As it would seem, we are declaring an integer variable called height and assigning it a value of zero. Again, it is a matter of convention that variable names are not capitalized. Variables declared in this way just inside a class definition (and not inside the definition of a method, such as main, as we saw in previous examples) become properties for Objects of the class. Thus, this line says that every Object of class Tree will have a property called height that will contain an integer value, and that the initial value of the height property will be zero. The word public indicates that any code in your Java program can view and modify the value in this property. In part two of this article, we'll see techniques for protecting data stored in an Object's properties, but for now this will suffice.

That's actually all there is to creating a Tree class that will keep track of its height; however, to make this example at least a little interesting, we'll also implement the grow method that I mentioned in the previous section. It begins with the following:

9     /**  
10     * Grows this tree by 1 meter  
11     */  
12    public void grow() {

Let me explain this line one word at a time. The word public indicates that the grow method (operation) is publicly available, meaning that it may be triggered by code anywhere in the Java program. Methods may also be private or protected, and I'll explain the meaning of each of these options later on. The word void indicates that this method will not return a value. Later on we'll see how to create methods that produce some value as an outcome, and for such methods we would replace void with the type of value to be produced (e.g. int).

Finally, the word grow is the actual name of the method that is to be created. Note that I am observing the convention of spelling method names starting with a lowercase letter. The empty parentheses following this word indicate to Java that it is a method we are declaring (as opposed to another property, like height above). Later on we'll see cases where the parentheses are not empty. Finally, the opening brace signifies the start of the block of code that will be executed each time the grow method of a Tree Object is triggered.

13      height = height + 1;

This operation happens to be a simple one. It takes the value of the height property and adds one to it, storing the result back into the height property. Note that we did not need to declare height as a variable in this method, since it has already been declared as a property of the Object on line 7 above. If we did declare height as a variable in this method, Java would treat it as a separate variable created anew every time the method was run, and our class would no longer function as expected (try it later if you're curious).

14   }  
15 }

The closing brace on line 14 marks the end of the grow method, while that on line 15 marks the end of the Tree class. After typing all this in, save the file and then compile it as you would a Java program:

C:\JavaTree> javac Tree.java

As you might expect, a file called Tree.class is created in the same directory. This is the compiled definition of the Tree class. Any Java program that you try to create a Tree in will look for this file to contain the blueprint of the Object to be created. In fact, that's our next step.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links