Following code will result in: class A { public static void main(String [] args) {A a = new B(); }} class B extends A {}
-
Compiler error
-
Runtime Exception
-
No errors
This is a valid upcast. Variable 'a' is declared as type A (the parent class), and it's assigned an instance of B (the child class). This is always valid because a child class 'is-a' parent class through inheritance - the child has all the members the parent has.
To answer this question, let's go through each option to understand why it is correct or incorrect:
Option A) Compiler error - This option is incorrect because there are no syntax or compilation errors in the given code. The code compiles successfully.
Option B) Runtime Exception - This option is incorrect because there are no runtime exceptions thrown in the given code. The code executes without any errors.
Option C) No errors - This option is correct because there are no syntax errors or runtime exceptions in the given code. The code compiles successfully and executes without any issues.
The correct answer is C) No errors. This option is correct because the code is syntactically correct and executes without any errors.
Therefore, the code will result in no errors.