Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
|
| Review Questions |
| 7.35 | What will be the result of compiling and running the following program?
public class Polymorphism {
public static void main(String[] args) {
A ref1 = new C();
B ref2 = (B) ref1;
System.out.println(ref2.f());
}
}
class A { int f() { return 0; } }
class B extends A { int f() { return 1; } }
class C extends B { int f() { return 2; } }Select the one correct answer.
|
| 7.36 | What will be the result of compiling and running the following program?
public class Polymorphism2 {
public static void main(String[] args) {
A ref1 = new C();
B ref2 = (B) ref1;
System.out.println(ref2.g());
}
}
class A {
private int f() { return 0; }
public int g() { return 3; }
}
class B extends A {
private int f() { return 1; }
public int g() { return f(); }
}
class C extends B {
public int f() { return 2; }
}Select the one correct answer.
|
| 7.37 | Which statements about the program are true?
public interface HeavenlyBody { String describe(); }
class Star {
String starName;
public String describe() { return "star " + starName; }
}
class Planet extends Star {
String name;
public String describe() {
return "planet " + name + " orbiting star " + starName;
}
}Select the two correct answers:
|
| 7.38 | Given the following code, which statement is true?
public interface HeavenlyBody { String describe(); }
class Star implements HeavenlyBody {
String starName;
public String describe() { return "star " + starName; }
}
class Planet {
String name;
Star orbiting;
public String describe() {
return "planet " + name + " orbiting " + orbiting.describe();
}
}Select the one correct answer:
|
| 7.39 | Which statement is not true?
Select the one correct answer.
|