Single Choice Not Set

QWhat is the output of this program?
     class A 
    {
        int i;
        void display() 
        {
            System.out.println(i);
        }
    }    
    class B extends A 
    {
        int j;
        void display() 
        {
            System.out.println(j);
        }
    }    
    class inheritance_demo 
    {
        public static void main(String args[])
        {
            B obj = new B();
            obj.i=1;
            obj.j=2;   
            obj.display();     
        }
   }

ID: #2246 Inheritance in Java MCQ 13,331 views
Question Info
#2246Q ID
Not SetDifficulty
Inheritance in Java MCQTopic

Choose the Best Option

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

  • B 1
  • C 2
  • D Compilation Error
Correct Answer

Explanation

Class A & class B both contain display() method, class B inherits class A, when display() method is called by object of class B, display() method of class B is executed rather than that of Class A. output:
$ javac inheritance_demo.java $ java inheritance_demo 2

Share This Question

Challenge a friend or share with your study group.