What will be the output of the following Java program? [code] abstract class A { int i; abstract void display();

What will be the output of the following Java program?
Code:
    abstract class A 
    {
        int i;
        abstract void display();
    }    
    class B extends A 
    {
        int j;
        void display() 
        {
            System.out.println(j);
        }
    }    
    class Abstract_demo 
    {
        public static void main(String args[])
        {
            B obj = new B();
            obj.j=2;
            obj.display();    
        }
    }
A. 0
B. 2
C. Runtime Error
D. Compilation Error
 
Answer: b
Explanation: class A is an abstract class, it contains a abstract function display(), the full implementation of display() method is given in its subclass B, Both the display functions are the same. Prototype of display() is defined in class A and its implementation is given in class B.
output:
Code:
$ javac Abstract_demo.java
$ java Abstract_demo 
2
 

Tìm kiếm

Back
Top