Menu
Topics Index
...
`

Product Overidding2644


If you need explanation Read this topic

If you need Answer Take test on this topic

Product Overidding2644
What will be the output of the following program?
class Product {
    public static void main(String[] args) {
        M m = new M();    M n = new N();
        M o = new O();    O oo = new O();
        m.product(3);    n.product(3);
        oo.product(3);
    }
}
class M {
    int product(int i) {
        int result = i * i;
        System.out.print("{" + i + ", " + result + "}~");
        return result;
    }
}
class N extends M {
    int product(int i) {
        int result = super.product(i) * super.product(i);
        System.out.print("[" + i + ", " + result + "]~");
        return result;
    }
}
class O extends M {
    int product(int i) {
        int result = super.product(i) * super.product(i);
        System.out.print("(" + i + ", " + result + ")~");
        return result;
    }
}

A. {3, 9}~[3, 9]~(3, 9)~
B. {3, 9}~[3, 9]~{3, 9}~(3, 27)~
C. {3, 9}~{3, 9}~{3, 9}~[3, 81]~{3, 9}~(3, 27)~
D. {3, 9}~{3, 9}~{3, 9}~[3, 81]~{3, 9}~{3, 9}~(3, 81)~
E. Some other output
F. Compilation Error or Runtime Error
Topic: Super Keyword In Java To Call Super Class Constructor

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

c

Posted by Kick Bhasha    2014-01-28 13:05:44


Answer is D,
First class M product() method executes and prints {3,9}~ and then class N and O product execute in each super is called so M product executes prints {3,9}~ twice as two times calling super and again in their respective method once {3,81}~ will be printed.

Posted by Amar Kumar Biradar    2014-01-28 13:08:08


Ans D

Posted by Pranavi Prakash    2014-01-28 14:18:45


D

Posted by Pavan Prince G    2014-01-28 16:07:31


Congratulations Amar Kumar Biradar. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-01-29 04:50:00


Thanks Merit Campus ;)
Got Recharge :)

Posted by Amar Kumar Biradar    2014-01-29 04:58:11


© meritcampus 2019

All Rights Reserved.

Open In App