The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
1k views

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
asked in Programming by Veteran (69k points)
retagged by | 1k views
Answer this question please...
ans is D

 

using System;

public class A
{
  public void f(int i) {                
       
Console.WriteLine(i);    
     
    }
}

public class B:A
{
    public    void f(int i) {    
         Console.WriteLine(2*i);      
    }
}
public class C
{
       public static void Main()
    {
    A x = new B();

    B y = new B();

    A z = new B();
    
    x.f(1);
   ((A)y).f(1);
    z.f(1);
    
    }
 i think answer should be 1,1,1

Here A x = new B(); here x refers to base class method...

 B y = new B(); here y refer to derived class method but it is casted to Base class .so again refer to Base Class.

   A z = new B();as again as First 1  z refer to base class method.

Hi , Please pardon my ignorance.

But according to question given , isn't it :

public class A
{
  public void f(int i) {                
       

   Console.WriteLine(i);        
    }
}

public class B:A // As class B is subclass of A
{
    public    void f(int i) {    
       Console.WriteLine(2*i);      
    }
}

 

 

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

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

this is actually the class definitions:-

2 Answers

+2 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);      
    }  
}

 

answered by Loyal (3.6k points)
0 votes
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)
answered by (445 points)
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.
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
BTW why the second print was "h"

can someone explain me??

Someone good with java


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

33,687 questions
40,230 answers
114,268 comments
38,793 users