Menu
Topics Index
...
`

Product Overidding2952


If you need explanation Read this topic

If you need Answer Take test on this topic

Product Overidding2952
What will be the output of the following program?
public 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 = i + i;
        System.out.print("[" + i + ", " + result + "]~");
        return result;
    }
}
class O extends M {
    int product(int i) {
        int result = i * 2;
        System.out.print("(" + i + ", " + result + ")~");
        return result;
    }
}

A. {3, 9}~[3, 9]~(3, 9)~
B. {3, 9}~[3, 6]~(3, 6)~
C. Compilation Error
D. Runtime Error
Topic: Method Overriding In Java

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.

Ans is Option B..... here the execution start from main() method after execution of first stmt it will create object for Class M with the variable "m" and also it will create Object of Class N and is stored in the reference variable of class M with "n"... here if you create Object of sub class then it will automatically create Object of super class bcz inside the constructor of sub class we have one default stmt i.e super()... due to this stmt it will call the super class construtor so it will create object for super class also.... simillarly after execution of second stmt it will create Object of Class O and it is stored inside the reference variable of Class M with variable "o" and Object of Class O with variable "oo" after that it will call the method product(3) inside the Class M  then i is initialized with the value i=3... and the variable result is initialized with the value 9 bcz result=i*i=> result=9 so it will print the values of i and result as similar to println stmt like{3, 9}~ and then it will return to main() method and again it will cal method product(3) of Class N  bcz if you have same method names in super class and sub class are same (i.e method override) then it will cal the sub class method  so i initialized with 3 i=3... and result is initialized with 6 bcz result=i+i=> result=6 and it will print i and result value as simillar to println stmt [3, 6]~ and it will return to main() method again it will call method product(3) inside the Class O and i is initialized with 3 i=3 and result initialized with 6 result=6 bcz result=i*2=> result =6 then again it will print the values of i and result  as simillar to println stmt as (3, 6)~.... so the Ans is Option B...

Posted by Bhagi Bhagyasri    2015-01-20 04:56:51


i'll go with option B... here when execution starts from main .. 4 objects will be created m,n,o,oo, in succeeding statements only product methods of m,n,oo are called so Object of M class m's product method results in  { 3,9}~
.. next Object of N class n's product method results in  [3,6]~  
.. finally Object of O class oo's product method results in  (3,6) ~
..According to me this Program Makes us to Know .. the 3 things ...
SubClass Object Can be Saved in SuperClass Varable (Reference)
As well as its own Class Variable ..
And also even though we are calling Super class Reference variable's method .. due to Overriding of methods the SubClass Method is Called ..
So.. even m,n are Class M variables .. Output is According to the Saved Object..

Posted by ?????????? ?????    2015-01-20 06:13:18


b.

Posted by Shubham Bansal    2015-01-20 07:19:12


This dose is now closed and the winners are Bhagi Bhagyasri,  for 'First Correct Comment', Bhagi Bhagyasri,  for 'Best Comment' and Shubham Bansal for the 'Popular Comment'. The 'lucky liker' is ?????????? ?????. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-22 07:45:44


© meritcampus 2019

All Rights Reserved.

Open In App