Name : ___________________________ Date : _________________ 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{ } //test code in the main method A one = new A(); out.println(one); one = new B(); out.println(one);
2. What is the output of the code at right?
class C{ private int x; public C() { x =3;} public void setX(int val){ x=val; } public int getX(){ return x; } public String toString() { return ""+getX(); } } class D extends C{ } //test code in the main method C two = new C(); out.println(two); two = new D(); two.setX(9); out.println(two); two.setX(-12); out.println(two.getX());
3. What is the output of the code at right?
class E{ private int x; public E() { x =3;} public E(int val){ x=val; } public String toString() { return ""+x; } } 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);
©A+ Computer Science – Inheritance worksheet - www.apluscompsci.com
Name : ___________________________ Date : _________________ Inheritance Worksheet 2
Use the classes at right to evaluate the code that follows. If the test code would generate and error, write ERROR. Each question should be evaluated in isolation.
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(); } }
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);
©A+ Computer Science – Inheritance worksheet - www.apluscompsci.com
Name : ___________________________ Date : _________________ 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(); } } //test code in the main method J one = new J(); out.println(one); one = new K(); one.fun(); out.println(one);
2. What is the output of the code at right?
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 super.back(); } public double back() { return 2; } public String toString() { return super.toString(); } } //test code in a main method M two = new M(); out.println(two.go()); out.println(two.back()); out.println(two.fun()); out.println(two); two = new N(); out.println(two.go()); out.println(two.back()); out.println(two.fun()); out.println(two);
©A+ Computer Science – Inheritance worksheet - www.apluscompsci.com
Name : ___________________________ Date : _________________ Inheritance Worksheet 4 1. What is the output?
class P { private int x, y; public P() { x=7; y=0; } public P(int v){x=v; y=7;} public double fun() { return x; } public void go() { back(); } public void whoot() { go(); } public void back() { x=992; } public String toString() { return getClass().getName() + " " }
+ x + " " + y;
} class Q extends P { private int x; public Q() { x=23; } public Q(int v) { super(v); x=33;
}
public double fun() { return x; } public void go() { back(); } public void back() { x=45; } public String toString() { return "class " + getClass().getName() + " " + x + " " + super.toString() ; } } //code int the main of another class P one = new P(); out.println(one.fun()); one.go(); one.whoot(); System.out.println(one+"\n\n" ); one = new Q(); out.println(one.fun()); one.go(); one.whoot(); System.out.println(one);
©A+ Computer Science – Inheritance worksheet - www.apluscompsci.com
Name : ___________________________ Date : _________________ 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; } } //test code in the main method J one = new J(); one = new J(); one = new J(); out.println(one); one = new J(); one = new J(); one = new J(); one = new J(); one = new J(); out.println(one);
2. What is the output of the code at right?
class M{ private int x, y; private static int c;
Static Static means only one. The inheritance slides contain static examples.
//all M share c
public M() { c++; } public double fun() { return 5; } public double back() { return fun(); } public String toString() { return x + " " + y + " " + c; } public static int count() { return c; } } 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(); } } //test code in a main method M two = new M(); out.println(M.count()); two = new M(); two = new N(); out.println(two.fun()); two = new N(); out.println(((N)two).go()); two = new M(); two = new N(); out.println(two); out.println(N.count());
©A+ Computer Science – Inheritance worksheet - www.apluscompsci.com
Name : ___________________________ Date : _________________ Inheritance Worksheet 1 Directions :: Extend class Monster to make a Gargoyle. A Gargoyle is a Monster. A Gargoyle has all of the properties and behaviors of a Monster and it has an 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( ) method.
public class Monster { private String myName; public Monster() { myName = "Monster"; } public Monster( String name ) { myName = name; } public String toString() { return myName + "\n"; } }
©A+ Computer Science – Inheritance worksheet - www.apluscompsci.com