0% found this document useful (0 votes)
5 views1 page

Inheritance Worksheet2 Java Aplus Key

The document is a worksheet focused on inheritance in Java, featuring classes G and H. It includes code snippets that demonstrate object creation, method calls, and the effects of inheritance on method accessibility. The output of various print statements is provided, illustrating the behavior of the classes and their interactions.

Uploaded by

chintuhosalli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Inheritance Worksheet2 Java Aplus Key

The document is a worksheet focused on inheritance in Java, featuring classes G and H. It includes code snippets that demonstrate object creation, method calls, and the effects of inheritance on method accessibility. The output of various print statements is provided, illustrating the behavior of the classes and their interactions.

Uploaded by

chintuhosalli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

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+" "+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);

G three = new H();


out.println(three);
three.setX(8);
three.setY(21); ERROR – G has no setY
out.println(three);

G four = new H();


four.setX(11);
out.println(four);
four.setX(6); 4 11
((H)four).setY(14); 14 6
out.println(four);

H five= new H();


five.setY(7);
out.println(five);
five.setX(16);
five.setY(9); 7 3
out.println(five); 9 16

© A+ Computer Science – Worksheet – www.apluscompsci.com

You might also like