Multiple choice technology programming languages

Following code will result in: class A { public static void main(String [] args) {B b = new A(); }} class B extends A {}

  1. Compile error

  2. Runtime Exception

  3. No error

  4. Rintime Error

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The code attempts to assign a superclass object (A instance) to a subclass reference (B b). Even though B extends A, an A object is not a B. Java forbids this unsafe assignment at compile-time. You cannot assign a parent class instance to a child class reference without explicit casting.