CS 1401, Exam #4, Typical mistakes

Problem 2: Several students defined several fields in a superclass as private, and then directly accessed them in a subclass. This is not possible. To access these fields, you have to either define the fields in a superclass as protected (then you can access them directly), or use accessor ("getter") methods to access these fields in a subclass.

Problem 2: if a method in a subclass is exactly the same as in the superclass, there is no need to repeat it in the description of the subclass -- it will automatically be there; this is the whole idea behind inheritance. A typical mistake is to explicitly add these method.

Problem 2: overwriting means creating a new method which has exactly the same header as the old one, i.e., the same name, the same type of output, and the same type of variable. A typical mistake was to use a different name for the new method.

Problem 5: if, in a class, try and catch are placed outside the method (as some students did), this will not affect the method at all: when you call a method, Java compiler simply looks for a method with this name and ignore whatever code is around it. Problem 6: when looking for the largest element in an array h, several students initialized the value of the largest-so-far variable with 0 (instead of the recommended value h[0]). This initialization will not work if all the elements of the array are negative: then the largest-so-far remains 0, while the largest element of the array is negative.

Similarly, when looking for the smallest element, some students initialized the value of the smallest-so-far variable to some large number, e.g., 1,000. This will not work if all the elements of this array are larger than 1,000.