This question may be considered too advanced for this exam. Given:
class MyBaseClass{ public MyBaseClass (int i){ } } abstract class MyClass extends MyBaseClass{ public MyClass (int i){ super (i); } public abstract void m1 (); } public class MyTest{ public static void main (String [] args){ MyClass ms = new MyClass (){ public void m1 () { System.out.println ("In MyClass .m1 ()"); } };/*from ww w. j a va2s .c om*/ ms.m1 (); } }
What will be the output when the above code is compiled and run?
Select 1 option
Correct Option is : A
When you define and instantiate an anonymous inner class for an abstract class, the anonymous class is actually a subclass of the abstract class.
Since the anonymous class does not define any constructor, the compiler will add the default no-args constructor to the anonymous class.
It will try to call the no-args constructor of its super class MyClass.
But MyClass doesn't have any no-args constructor.
Therefore, it will not compile.
The anonymous inner class should have been created like this:.
MyClass ms = new MyClass ( someInteger ){ ... };