Visibility Reviewed in Java


Program items to watch out for - review the style guides if questions - general

== true/false
A boolean variable is true or false. To check if they are == true or == false is an added unnecessary step.

Ex: if (started == true && completed == false) { do something}
S/B: if (started && !completed) {do something}

Unnecessary initializations
Only initialize a variable if it is needed on the right of an assignment, is used in a condition, or is initialized as part of a try where that initialization may not make sense in a catch.

Ex: Accumulators need to be initialized before the loop. Loop control variables need a starting point.
Not: You have a separate series of if's without a final else or an if/else structure without a final else. See if/else structures.

if/else
If you have several conditions each of which describe a "case", you should have an if -- else if -- else if -- else, where each of the conditions is described in the if's and the last condition is your default. If there is no else, in other words if -- else if -- else if describe all of the possible states or conditions, then change the last to an if else. Or use defensive programming and create an error message else.
Spacing
Return statements
Appropriate use of try/catch
Do not use it where prevention is best. Array index out of bounds is not a good use of try/catch. Must use it when dealing with file I/O. Prevention is best with run-time exceptions.
Acknowledgement statement
All program files must contain a "reference" section or "acknowledgement" section as a comment (not a Javadoc format comment) which states either that you had no help on the assignment other than professor or book, that you received help from a TA (include all TA's, names, and what code they helped you with), or other reference help (Internet, library, etc.) If no help was received, a statement to that effect is required.

Specific items to look for in PA3

  1. Make sure that each class contains a Javadoc style comment including @author and @version tags.
  2. Make sure each method contains a Javadoc style comment including @param and @return tags. If throwing an exception, include @throws.
  3. Make global only those variables that describe attributes pertaining to the state of the object or to several methods.
  4. Keep it simple. Try to design your classes to take advantage of reuse.

Lab Followup - Inheritance

  1. Constructors - use of super to call the parent constructor.
  2. Method overriding - a child has a different version of a parent method. Which one is used? Why did we need to call the super class's updateTime method?
  3. Can we assign to a Clock variable an AlarmClock object? Vice versa?
  4. Why when we built an AlarmClock object could we not call its setAlarm method?

Revisit Exception Hierarchy - Chap 10

Diagram on page 542. Notice the inheritance hierarchy. RunTimeExceptions are not checked. All others are.

Today's notes - Accessibility and Visibility

Terminology

overriding
The process of modifying the definition of an inherited method to suit the purposes of the subclass.
overloading
The process of assigning additional meaning to a programming language construct, such as a method or operator. In Java, method overloading is supported.
shadowing
A child class declares variables with the same name and type as the parent.
visibility
The ability of one entity to "see" another.
types of visibility
dereference
To obtain the value that the pointer is pointing to. In Java, we refer to dereferencing when we use an object to refer to one of its characteristics (members).

Examples

StaticMethods1.java

Reference.java

SubprogramAvailability1.java

BlockAvailability1.java

Weight.java

and more....


CS 239 - Spring 2006
Department of Computer Science Nancy Harris Home Page Current Classes Link