Home » Java Basics » 04 - Class and Object Concepts
4

A Typical Java Class

A sample of a typical Java class

Take a look at the file ‘Fruit.Java’. This is a simple non-visual class, Fruit.java simply contains a model of a simple object. Open a new class file with the name 'Fruit.java' in Netbeans 5.5 under the MySamples project folder. Replace its contents entirely with the code in Fruit.java, save your copy of fruit.java and compile the file.

Let us go through the Fruit.java file one section at a time.

  1. The following are the properties of the Fruit Object: a name, a set of viable colors (limited to 10 - the length allocated for the string array), a string that holds the name of an image, and the Fruit's calorific value.
    String name;
String[] color = new String[10] ;
String image;
int calories;
  1. Now, take a look at the Constructors of the Fruit class. A constructor is used to create a new instance of an object (a new fruit). The following constructor takes four input parameters - a name, a list of colors, an image name, and the number of calories and creates a new Fruit Object by setting the properties equal to the input parameters.
     public Fruit(String name,String[] color,String image,int calories) {
this.name = name ;
this.color = new  String[color.length];
System.arraycopy(color,0,this.color,0,color.length);
this.image = image ;
this.calories = calories ;
}
  1. The arraycopy method is used to set the String array 'color' property of the object equal to the array passed in through the input parameter (note that ‘this.color’ refers to the object's property).
  2. Constructor methods always have the same name as the class. Also, notice that the ‘this’ keyword is used to the object being created. Any property or method of the current object may be referenced using the 'this' keyword within the object's class.
  3. The Fruit class has three constructor methods, and this is by design. Although the methods obviously have the same name, they take in different input parameters. One takes all four properties, another takes everything but the image parameter, and yet another constructor takes just the name and colors. This offers flexibility to the users; a Fruit may be created through a few different methods. Any method may be overloaded in this way. The Fruit class includes overloaded constructors just to provide a practical example of the concept. Overloading is very useful in spite of the fact that it makes the code more obscure. Note that overloading a method is very different from overriding a method. Suppose the super class (the class the current class extends) contains a method 'A'. The current class would inherit this method. The current class is said to override method A if the current class also contains its own version of method A. Overloading, on the other hand, refers to duplicate implementations of a single method 'B' with each implementation differing on the number or size of input parameters.

The second overloaded constructor in Fruit.java

    public Fruit(String name,String[] color,int calories) {
this.name = name ;
this.color = new  String[color.length];
if (color.length <= 10 )  {
System.arraycopy(color,0,this.color,0,color.length);
}
else   {
System.arraycopy(color,0,this.color,0,10);
}
this.image = "No Image" ;
this.calories = calories ;
}

Overloaded indexOf methods in Java.lang.String:

 int indexOf(int ch, int fromIndex)
Returns the index within this string of the
first occurrence of the specified character,
starting the search at the specified index
int indexOf(String str)
Returns the index within this string of the
first occurrence of the specified substring
int indexOf(String str, int fromIndex)
Returns the index within this string of
the first occurrence of the specified substring,
starting at the specified index
  1. The above example (from the javadoc) shows how the indexOf method may be invoked using different input parameters (a character and an integer index, a substring, or a substring and an integer index) according to the user's requirements.
  2. An error will result if the 'color' input parameter contains more than 10 because the size of the 'color' array is limited to 10 elements. An ‘if loops’ checks the size of the input array. If the size is less than or equal to ten, all colors are copied into the object's 'color' array. If the input color array contains more than ten elements, we simply copy the first ten elements and ignore the rest. The ‘if loop’ appears in every constructor and the setColor method.
  3. The following methods are informally called getters and setters. They perform no function other that getting the current value of a property or setting the current value of a property. The setter methods may be used to change the value of an instance variable. Other methods that describe the behavior of a Fruit Object (eatFruit, sliceFruit etc.) may also be written as the need arises.
public String getName() {
return this.name;
}
public String[] getColor() {
return this.color;
}
 
.
.
.
 
public void setName(String name){
this.name = name;
}
  1. Note that 'this.color' or 'this.name' denotes value of the color and name property of the instance on which the methods was invoked. The following is a code snippet that shows how the getter methods work.
   int papayaCalories, checkCalories;
String[] papayaColors = {"green","orange"};
//Papaya is an instance of fruit
Fruit papaya = new Fruit("Papaya", papayaColors, "papaya.jpg", 120);
.
.
(Some Code)
.
.
//Obtain papaya's calories
papayaCalories = papaya.getCalories()
checkCalories = papayaCalories   //checkCalories contains 120
 
  1. The getCalories method is invoked for papaya, an instance of the Fruit object.
  2. Once the getCalories method completes execution, control is transferred to the calling program. The 'return' keyword is used to send back 'this.calories' (initially set to 120 at the time of construction).

The showDetails method is a utility that allows the developer to print the properties of a fruit.

 
public void showDetails() {
System.out.println("Fruit Name: " + this.name)  ;
System.out.println("Fruit Colors: " )  ;
for(int i=0;i<color.length;i++)
System.out.println(color[i]);
System.out.println("Fruit Image File: " + this.image)  ;
System.out.println("Fruit Calories: " +
java.lang.String.valueOf(this.calories));
}

The showDetails method for the papaya fruit may be invoked in the following way:

    papaya.showDetails();

Output of papaya.showDetails

Fruit Name: Papaya
Fruit Colors:
green
orange
Fruit Image File: papaya.jpg
Fruit Calories: 120