Professional Documents
Culture Documents
Inheritance Worksheet 1
1. What is the output of the code at right? class A{
private int x;
public A() { x =3;}
public String toString() {
return ""+x;
}
}
class B extends A{
}
class D extends C{
}
class F extends E{
public F(){ }
public F(int num){
super(num);
}
}
//test code in the main method
E three = new E();
out.println(three);
three = new F(45);
out.println(three);
Inheritance Worksheet 2
class G{
private int x;
public G() { x=3;}
public void setX(int val){
Use the classes at right to evaluate the code x=val;
that follows. If the test code would generate }
and error, write ERROR. public String toString(){
return ""+x;
}
Each question should be evaluated in }
isolation.
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();
}
}
1. What is the output of the code at right? //test code in the main method
G one = new G();
out.println(one);
one.setX(5);
out.println(one);
H two = new H();
two.setX(-2);
two.setY(11);
out.println(two);
2. What is the output of the code at right? //test code in the main method
G three = new H();
out.println(three);
three.setX(8);
three.setY(21);
out.println(three);
3. What is the output of the code at right? //test code in the main method
G four = new H();
four.setX(11);
out.println(four);
four.setX(6);
((H)four).setY(14);
out.println(four);
4. What is the output of the code at right? //test code in the main method
H five= new H();
five.setY(7);
out.println(five);
five.setX(16);
five.setY(9);
out.println(five);
Inheritance Worksheet 3
1. What is the output of the code at right? 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() { out.println(back()); }
public String toString() {
return "class K " + super.toString();
}
}
class N extends M{
public N() { }
public double fun() { return 7; }
public double go() { return super.back(); }
public double back() { return 2; }
public String toString() {
return super.toString();
}
}
Inheritance Worksheet 4
class Q extends P
{
private int x;
Inheritance Worksheet 5
1. What is the output of the code at right? class J
{
private static int x; //only one
public J() { x++;}
public String toString() {
return ""+x;
}
}
class N extends M{
public N() { }
public double fun() { return 7; }
public double go() { return super.back(); }
public double back() { return 2; }
public String toString() {
return "class N " + super.toString();
}
}
Inheritance Worksheet 1
Directions :: Extend class Monster to make a Gargoyle. A Gargoyle is a Monster. public class Monster
A Gargoyle has all of the properties and behaviors of a Monster and it has an {
private String myName;
additional property of type double that stores the agility of the Gargoyle. You
must provide 3 constructors for class Gargoyle. You must provide a toString( ) public Monster()
method. {
myName = "Monster";
}