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
- Decision and loop structures should be separated from their
surrounding code by one line of whitespace.
- Variable declarations should be separated from the following code
by one line of whitespace.
- Parameter lists, either actual or formal should have no space
before the comma and one space after the comma.
- Binary operators should have one space before and after the
operator.
- And of course, 3 space indenting for all structures is the
norm.
- Return statements
- If a method is an accessor intending to return the current state
of an attribute, DO keep it simple and return the attribute.
- If a method is returning a method call result, it is okay to say
"return abc.getValue();"
- If a method is returning a simple calculation,
it is okay to say "return abc + xyz;"
- Multiple returns should be avoided (see recursion for an
acceptable use of multiple returns).
- 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
- Make sure that each class contains a Javadoc style comment including
@author and @version
tags.
- Make sure each method contains a Javadoc style comment including @param and @return tags. If
throwing an exception, include @throws.
- Make global only those variables that describe attributes pertaining to
the state of the object or to several methods.
- Keep it simple. Try to design your classes to take advantage of
reuse.
Lab Followup - Inheritance
- Constructors - use of super to call the
parent constructor.
- 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?
- Can we assign to a Clock variable an AlarmClock object? Vice versa?
- 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
- block - entity is created and accessible within a single block of
code
- subprogram - entity is created and accessible with a
subprogram
- Class/private - entity is visible to all entities in the same
class
- Generalized/proteceted - entity is accessible to its class and
all extensions of the class
- Complete/public - entity is accessible to all classes
- 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