-
Consider the following class definition in a hypothetical object oriented language that supports inheritance and uses dynamic binding. The language should not be assuemed to be either Java or C++, though the syntax is similar.
Class P { Class Q subclass of P { void f (int i) { void f (int i) { print (i); print (2*i);
}
}
Now, consider the following program fragment”
Px = new Q()
Qy = new Q();
Pz = new Q();
X.f(1); ((P)y). f(1); z.f(1);
Here, ((P)y) denotes a type cast of y to P. The output produced by executing the above program fragment will be
-
- 1 2 1
- 2 1 1
- 2 1 2
- 2 2 2
- 1 2 1
Correct Option: D
1. Px = newQ();
2. Qy = newQ();
3. Pz = newQ();
4. x : f (1); print 2 # i = 2
5. ((P) y) : f (1);
6. z : f (1) print 2 # i = 2
but line 5. will print 2 because typecast to parent class can't prevent over ridding. So function f(1) of class Q will be called not f (1) of class P.
Hence (d) is correct option.