A+ Computer Science – Inheritance Worksheet 2
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 G{
private int x;
public G() { x=3;}
public void setX(int val){
x=val;
}
public String toString(){
return ""+x;
}
}
class H extends G{
private int y;
public H() { y=4;}
public void setY(int val){
y=val;
}
public String toString() {
return ""+y+" "+[Link]();
}
}
////////////////////////////////////////////
//test code in the main method
G one = new G();
[Link](one);
[Link](5);
[Link](one); 3
H two = new H(); 5
[Link](-2); 11 -2
[Link](11);
[Link](two);
G three = new H();
[Link](three);
[Link](8);
[Link](21); ERROR – G has no setY
[Link](three);
G four = new H();
[Link](11);
[Link](four);
[Link](6); 4 11
((H)four).setY(14); 14 6
[Link](four);
H five= new H();
[Link](7);
[Link](five);
[Link](16);
[Link](9); 7 3
[Link](five); 9 16
© A+ Computer Science – Worksheet – [Link]