What will be the output of the following Java program? [code] class X { int a; double b; } class Y extends

Lâm Phung

New member
What will be the output of the following Java program?
Code:
    class X 
    {
        int a;
        double b;
    }
    class Y extends X 
    {
	int c;
    }
    class Output 
    {
        public static void main(String args[]) 
        {
            X a = new X();
            Y b = new Y();
            Class obj;
            obj = b.getClass();
            System.out.print(obj.getSuperclass());
        }
    }
A. X
B. Y
C. class X
D. class Y
 
Answer: c
Explanation: getSuperClass() returns the super class of an object. b is an object of class Y which extends class X , Hence Super class of b is X. therefore class X is printed.
Output:
Code:
$ javac Output.java
$ java Output
class X
 

Tìm kiếm

Back
Top