What will be the output of the following Java program?

final class A 
   {
        int i;
   }    
   class B extends A 
   {
       int j;
       System.out.println(j + " " + i);  
   }    
   class inheritance 
   {
       public static void main(String args[])
       {
           B obj = new B();
           obj.display();     
       }
  }

a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error

1 thought on “What will be the output of the following Java program?”

  1. d
    Explanation: class A has been declared final hence it cannot be inherited by any other class. Hence class B does not have member i, giving compilation error.
    output:
    $ javac inheritance.java
    Exception in thread “main” java.lang.Error: Unresolved compilation problem:
    i cannot be resolved or is not a field

Leave a Comment