MCQ Single Best Answer Not Set

QWhat is the output of this program?
      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();     
        }
   }

ID: #2248 Inheritance in Java MCQ 17,287 views
Question Info
#2248Q ID
Not SetDifficulty
Inheritance in Java MCQTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • A 2 2
  • B 3 3
  • C Runtime Error
  • D Compilation Error
Correct Answer: Option 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

Share This Question

Challenge a friend or share with your study group.