in Programming retagged by
4,539 views
2 votes
2 votes

Consider the following class definitions in a hypothetical Object Oriented language that supports inheritance and uses dynamic binding. The language should not be assumed 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:

P x = new Q();

Q y = new Q();

P z = new Q();

x.f(1); ((P)y).f(1); z.f(1);

Here ((P)y) denotes a typecast of y to P. The output produced by executing the above program fragment will be

  1. 1 2 1
  2. 2 1 1
  3. 2 1 2
  4. 2 2 2
in Programming retagged by
4.5k views

4 Comments

yes you are right..sorry its mistake while putting print statement..now i edited that
0
0

when ((A)Y).f(1) will be executed.. which f() function should be called?

this is actually the class definitions:-

0
0
out of syllabus for 2021 gate.
0
0

2 Answers

3 votes
3 votes

Answer is D)

Inheritance will make sure every function in parent class P is also a part of Q. But since Q has overridden the function f, Q's definition of function f will be used whenever an object of type Q is used to call f.

Dynamic binding determines the type of the object at run-time and not compile time. Refer  http://www.javatpoint.com/static-binding-and-dynamic-binding

Looking at the pseudo code, in all the cases x.f(1), ((P) y), z.f(1), the objects are of type Q, hence function f from child class will be used. And since the definition of function f is present in parent class as well there wont be any exceptions.

Here's a small java example.

public class HelloWorld{
    public static void main(String []args){
        P x = new Q();
        Q y = new Q();
        P z = new Q();
        P w = new P();
        x.f(1);
        ((P) y).f(1);
        z.f(1);
    }  
}    
public class P {
    void f(int i) {
        System.out.println(i);
    }
}
public class Q extends P {
    void f(int i) {
        System.out.println(2*i);      
    }  
}
1 vote
1 vote
Answer-C)

this is polymorphism

Superclass object = new subclass(); <<-- object created for subclass referenced by a superclass

when anything invoked using this object (THIS IS IMPORTANT)

All the methods of superclass can be invoked which are not in subclass

AND when a method is invoked which is in both (overridden) the SUBCLASS Method will be called!!

Additional info-->

superclass to subclass requires EXPLICIT CASTING

subclass to superclass doesn't require explicit casting (although no problem if used)

3 Comments

edited by
I read about polymorphism. It states that function calls are resolved at runtime. It does not depend upon the type of pointer(base or derived) used for calling purpose. It depends solely on the fact which object this pointer is pointing to. So lets say A is base class and B is derived class. we create a pointer of type A which is pointing to object of class B. so B class function will be called at runtime, not of A although pointer is of class A.

what I believe is typecasting here would not have any effect . Since all the pointers point to derived class objects so Only derived class function will be called.So answer should be 2 2 2

How about answer being (D). Discuss this.
1
1
Yes u are correct it should be D

damm my java concepts are broken :((

here is a code

class g{
    void he(){
        System.out.println("g");
    }
}
class h extends g{
    void he(){
        System.out.println("h");
    }
}
public class Nitin{
public static void main(String args[])
{
    h obj = new h();
    ((g)obj).he();
    ((g)new h()).he();
}

OUTPUT:

h

h
1
1
BTW why the second print was "h"

can someone explain me??

Someone good with java
0
0
Answer:

Related questions