Java Midterm II Practice
Extra Practice Sheet: Inheritance, Polymorphism & Overriding
Spring 2026
This sheet is for extra practice on concepts from Midterm 2 (Questions 2 and 3 only).
It is NOT a complete study guide — review lecture notes, lab exercises, and the full reference sheet.
Attempt all questions before checking the answers at the back.
Question 2
Given the following class hierarchy diagram, answer the questions below.
Given the declarations:
Java
Fruit fruit = new GoldenDelicious();
Orange orange = new Orange();
a. Is fruit instanceof Fruit true?
b. Is fruit instanceof Orange true?
c. Is fruit instanceof Apple true?
d. Is fruit instanceof GoldenDelicious true?
e. Is fruit instanceof Macintosh true?
f. Is orange instanceof Orange true?
g. Is orange instanceof Fruit true?
h. Is orange instanceof Apple true?
i. Suppose Apple contains a method called makeApple(). Can fruit invoke makeApple()?
j. Suppose makeOrangeJuice() is defined in Orange. Can orange invoke this method? Can fruit invoke
this method?
k. Is the statement Orange p = new Apple(); legal?
l. Is the statement Macintosh p = new Apple(); legal?
m. Is the statement Apple p = new Macintosh(); legal?
Question 3
Consider the following classes:
Java
public abstract class Animal {
public String action() {
return "lives";
}
public String toString() {
return "This animal " + [Link]();
}
}
public class Bird extends Animal {
private boolean flies;
public Bird(boolean canFly) {
super();
[Link] = canFly;
}
public boolean canFly() {
return [Link];
}
public String action() {
String result = [Link]();
if ([Link]) {
result = result + " and flies";
}
return result;
}
}
public class Parrot extends Bird {
public Parrot() {
super(true);
}
public String action() {
return [Link]() + " and talks";
}
}
public class Coral extends Animal {
public Coral() {
super();
}
public String toString() {
return "This can't be an animal";
}
}
For each of the following pieces of code, write what will be displayed to the console or clearly describe
the cause of any error (compile-time or runtime).
Java
Animal s = new Bird(true);
[Link]([Link]());
Java
Parrot p = new Bird(true);
[Link]([Link]());
Java
Animal c = new Coral();
[Link](c);
Java
Bird m = new Parrot();
[Link](m);
Java
Animal d = new Bird(false);
[Link](d);
Answers (check only after attempting the questions!)
Question 2 Answers
a. true
b. false
c. true
d. true
e. false
f. true
g. true
h. false
i. No (compile error — declared type Fruit does not declare makeApple())
j. Yes for orange; No for fruit (declared type Fruit does not declare makeOrangeJuice())
k. No (illegal — Apple is not a subclass of Orange)
l. No (illegal — cannot assign superclass to subclass variable)
m. Yes (legal — upcasting from subclass to superclass)
Question 3 Answers
1. Compile-time error — canFly() is not defined in the declared type Animal.
2. Compile-time error — incompatible types: cannot convert Bird to Parrot.
3. This can't be an animal
4. This animal lives and flies and talks
5. This animal lives