Section 11.10. Concepts Summary

[Page 379 (continued)]

11.10. Concepts Summary

In this chapter we have created classes with fields, constructors, and methods. We have shown how to create, initialize, and access an array. We have shown how to override an inherited method. We have shown how to use a debugger. We have shown how to use Javadoc comments to document a class. We explained how dynamic binding results in methods being invoked based on the class that created the current object.

11.10.1. Declaring a Class

To declare a class you use:

public class Student {   // all fields, constructors, and methods }

[Page 380]

This will declare the class Student, which will be a child of the class Object. You could have also declared this as:

public class Student extends Object {   // all fields, constructors, and methods }

This is using the extends keyword to explicitly say that this class inherits from the Object class. If no extends keyword is used, the class will automatically inherit from the class Object.

11.10.2. Fields

A field declaration looks very much like a local variable declaration except that it is inside the class definition and it should be preceded by the keyword private.

public class Student {    private String name;    private double[] gradeArray; }

Objects should protect their data from direct use by objects of other classes by making their data private.

11.10.3. Constructors

To declare a constructor you will usually use the keyword public followed by the class name and then a parameter list.

public Student() {}

The above constructor doesn't take any parameters and doesn't initialize the fields so they will have their default values. This is called a no-argument constructor. By default, numbers are initialized to 0, object references to null, and boolean fields to false. A no-argument constructor will automatically be added by the compiler, if you don't have any constructors declared in your class. But if you declare any constructors, the no-argument constructor won't be automatically added. In addition, if you don't have a call to a superclass constructor in your constructor, one will be added that calls the superclass no-argument constructor.

A constructor is always called when a new object is created. It is used to initialize the fields in the new object. You can have several constructors as long as the parameter lists are different. In other words, you can overload constructors just as you can overload methods.

Here is a constructor that takes a name for the new Student object:

public Student(String theName)  { = theName;  }

[Page 381]

11.10.4. Arrays

To create an array use the new keyword followed by a type and then the size in square brackets:

double[] gradeArray = new double[5];

You can create arrays of objects too:

Student[] studentArray = new Student[20];

You can also initialize the contents of the array when you create it by putting values inside of an open and close curly brace and separating the values with commas:

double[] gradeArray = {90.5, 23, 99, 87.5, 96};

11.10.5. Using a Debugger

Most IDEs have debuggers. A debugger is a tool that helps you see exactly what is happening in a program. The main thing that you do with a debugger is set breakpoints, which are places to stop and see what is happening.

Once you have stopped at a breakpoint you can examine the values of variables and fields. You can use STEP OVER, which executes the current line of code and then stops again. You can use STEP INTO, which will go into any method call on the current line and then stop. You can use STEP OUT to let a current method finish executing and stop again in the method that called the current method. You can use RESUME which will continue executing until another breakpoint or until execution of the program finishes.

11.10.6. Javadoc Comments

You should document your code to make it more reusable both by yourself and others. You should include at least a comment before the class definition and before each method.

A class comment should explain the purpose of the class and identify the author. Here is an example of a class comment:

/**  * Class that describes a student. A student has a name and an  * array of grades. You can get information about a student  * such as her/his name and grade average.  *  * @author Barb Ericson  */ public class Student

A method comment should describe the purpose of the method. It can list any preconditions that should be true before the method is called and any postconditions that will be true when the method has finished. It should also explain any parameters to the method and what is returned (if anything) from the method. Here is an example of a method comment:

[Page 382]

/**    * Method to get the grade in the grade array    * at the passed index    * @param index the index that we want the grade for    * @return the grade in the grade array at this passed index    */   public double getGrade(int index)

Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Year: 2007
Pages: 191 © 2008-2017.
If you may any questions please contact us: