Inheritance Worksheet2 Java Aplus Key
Inheritance Worksheet2 Java Aplus 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 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+" "+super.toString();
}
}
////////////////////////////////////////////
//test code in the main method
G one = new G();
out.println(one);
one.setX(5);
out.println(one); 3
H two = new H(); 5
two.setX(-2); 11 -2
two.setY(11);
out.println(two);