What will be the output of the following Java code?

 class A 
 {
     public int i;
     private int j;
 }    
 class B extends A 
 {
     void display() 
     {
         super.j = super.i + 1;
         System.out.println(super.i + " " + super.j);
     }
 }    
 class inheritance 
{
     public static void main(String args[])
     {
         B obj = new B();
         obj.i=1;
         obj.j=2;   
         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 code?”

  1. d
    Explanation: Class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it.
    output:
    $ javac inheritance.java
    Exception in thread “main” java.lang.Error: Unresolved compilation problem:
    The field A.j is not visible

Leave a Comment