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

Inheritance Worksheet3 Java Aplus Key

The document contains a worksheet on inheritance in Java, featuring classes J, K, M, and N with various methods and outputs. It includes test code that demonstrates the behavior of these classes, showing how method overriding and superclass methods interact. The expected outputs for each operation are provided, illustrating the principles of object-oriented programming.

Uploaded by

Destiny Sumani
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)
7 views1 page

Inheritance Worksheet3 Java Aplus Key

The document contains a worksheet on inheritance in Java, featuring classes J, K, M, and N with various methods and outputs. It includes test code that demonstrates the behavior of these classes, showing how method overriding and superclass methods interact. The expected outputs for each operation are provided, illustrating the principles of object-oriented programming.

Uploaded by

Destiny Sumani
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 3 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 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 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 the main method
J one = new J();
out.println(one); 3 3
one = new K(); 1
one.fun();
3 3
out.println(one);

M two = new M();


out.println(two.go()); 1.0
out.println(two.back()); 8.0
out.println(two.fun()); 8.0
out.println(two); 8 1
two = new N(); 7.0
out.println(two.go()); 2.0
out.println(two.back());
7.0
out.println(two.fun());
out.println(two) 8 1

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

You might also like