A+ Computer Science – Inheritance Worksheet 3 KEY
DIRECTIONS : Fill in each blank with the correct answer/output. Assume each
statement happens in order and that one statement may affect the next statement.
Some sections might print more than once.
class J{
private int x, y;
public J() { x = y = 3;}
public void fun() { x = y = 6; }
public int back() { return 1; }
public String toString() {
return x + " " + y;
}
}
class K extends J {
public void fun() { [Link](back()); }
public String toString() {
return "class K " + [Link]();
}
}
class M{
private int x, y;
public M() { x=8; y=1; }
public double fun() { return x; }
public double go() { return y; }
public double back() { return fun(); }
public String toString() {
return x + " " + y;
}
}
class N extends M{
public N() { }
public double fun() { return 7; }
public double go() { return [Link](); }
public double back() { return 2; }
public String toString() {
return [Link]();
}
}
//////////////////////////////////////////////
//test code in the main method
J one = new J();
[Link](one); 3 3
one = new K(); 1
[Link]();
3 3
[Link](one);
M two = new M();
[Link]([Link]()); 1.0
[Link]([Link]()); 8.0
[Link]([Link]()); 8.0
[Link](two); 8 1
two = new N(); 7.0
[Link]([Link]()); 2.0
[Link]([Link]());
7.0
[Link]([Link]());
[Link](two) 8 1
© A+ Computer Science – Worksheet – [Link]