final_sample_5 - CSE 142 Section Handout #9 Cheat Sheet...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 142 Section Handout #9 1 of 10 Cheat Sheet Inheritance (9.1 - 9.2) public class name extends superclass { inheritance : Forming a new class based on an existing class. extend : To inherit from another class. superclass : The "parent" class; the class being extended. subclass : The "child" class; the class extending another. override : To replace a superclass method with a new one. super keyword (9.3) Calling an overridden method from the superclass: super. method ( parameters ) Calling a constructor from the superclass: super( parameters ); Polymorphism puzzles public class Class1 { public void method1() { System.out.print("C1 m1 "); method2(); } public void method2() { System.out.print("C1 m2 "); } } public class Class2 extends Class1 { public void method1() { super.method1(); System.out.print("C2 m1 "); } public void method2() { System.out.print("C2 m2 "); } } The following would be the output from creating an object of each of the above classes and calling its methods: Class1 var1 = new Class1(); // Output: var1.method1(); // C1 m1 C1 m2 var1.method2(); // C1 m2 Class2 var2 = new Class2(); var2.method1(); // C1 m1 C2 m2 C2 m1 var2.method2(); // C2 m2 Other inheritance notes Subclasses cannot directly access private fields they inherit. Subclasses do not inherit constructors. If the superclass has one that requires parameters, the subclass must also. public class Employee { private int years; public Employee(int years) { this.years = years; } public int getHours() { return 40; } public double getSalary() { return 50000.0; } public int getVacationDays() { return 10 + 2 * years; } public int getYears() { return years; } } public class Lawyer extends Employee { public Lawyer(int years) { super(years); } public double getSalary() { return super.getSalary() + 5000.0 * getYears(); } public int getVacationDays() { return super.getVacationDays() + 5; } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 142 Section Handout #9 2 of 10 Sample Final Exam #5 (based on Autumn 2006's final; thanks to Ruth Anderson) 1. Array Mystery Consider the following method: public static void arrayMystery(int[] array) { for (int i = 0; i < array.length - 1; i++) { if (array[i] < array[i + 1]) { array[i] = array[i + 1]; } } } Indicate in the right-hand column what values would be stored in the array after the method mystery executes if the integer array in the left-hand column is passed as a parameter to mystery . Original Contents of Array Final Contents of Array int[] a1 = {2, 4}; arrayMystery(a1); int[] a2 = {1, 3, 6}; arrayMystery(a2); int[] a3 = {7, 2, 8, 4}; arrayMystery(a3); int[] a4 = {5, 2, 7, 2, 4}; arrayMystery(a4); int[] a5 = {2, 4, 6, 3, 7, 9}; arrayMystery(a5); _____________________________ _____________________________ _____________________________ _____________________________ _____________________________
Background image of page 2
CSE 142 Section Handout #9 3 of 10 2. Reference Semantics Mystery The following program produces 4 lines of output. Write the output below, as it would appear on the console. import java.util.*;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/15/2011 for the course CSE 142 taught by Professor Martystepp during the Spring '07 term at University of Washington.

Page1 / 10

final_sample_5 - CSE 142 Section Handout #9 Cheat Sheet...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online