What is the output of the following Java program?
interface A{
}
class B implements A
{
}
class C extends B
{
}
public class test
{
public static void main(String[] args)
{
A c=new C();
System.out.println(c instanceof B);
System.out.println(c instanceof A);
}
}
-
true
true
-
true
false
-
false
true
-
false
false
-
Error in the program
A
Correct answer
Explanation
A c=new C(); //parent class which is a interface reference variable refers to object of child class
Hence object 'c' created for child class using parent class reference variable.
System.out.println(c instanceof B); // B is the parent class of C hence c is the instanceof B also. Hence prints true.
System.out.println(c instanceof A); // A is implemented by child class B which is inherited again by child class C. Hence object 'c' can also implement interface A. Hence prints true.
Output is:
true
true