public class A {}
public class B extends A {
public void greet() { System.out.print("hi"); }
}
// Usage:
A obj = new A();
if (obj instanceof B) {
((B) obj).greet();
} else {
System.out.print("not B");
}What is printed?
- Acheck_circle
not B
- B
ClassCastException
- C
hi
- D
Compile error
Explanation
obj is a plain A, not a B, so instanceof B is false. The else branch prints "not B".