Forums Register Login

Polymorphism?

+Pie Number of slices to send: Send
Hi to All,

I think this question is about polymorphism? I have seen it in an interview exam and think knowing the answer will help me a lot. So here goes:



Which of the following assignments in the main() method will compile without error?

1. nc=bc;
2. o1=bc;
3. o2=bc;
4. all
[ July 31, 2007: Message edited by: Joseph Sweet ]
+Pie Number of slices to send: Send
4>All

all of the above will compile if you check the spelling of extends keyword.

and secondly,we are not playing quiz here.please try the question yourself before asking.this is not supposed to be a platform to give you examination drills

+Pie Number of slices to send: Send
 

Originally posted by Jitendra Jha:
4>All

all of the above will compile if you check the spelling of extends keyword.

and secondly,we are not playing quiz here.please try the question yourself before asking.this is not supposed to be a platform to give you examination drills...


Jitendra: Fortunately, this is not a quiz, because "all" is not correct. Only one of these will compile without error. Did you try this question yourself before answering?

Please remember the "be nice" rule.
[ July 31, 2007: Message edited by: marc weber ]
+Pie Number of slices to send: Send
How do you know I did not try to think about it before putting it here?

A question to the nicer members of this forum:
I think the third option (o2=bc) would not work, but it's hard for me to tell exactly why.

Could you explain this?
Or maybe some reference to a webpage that explains the principles behind this question?
[ July 31, 2007: Message edited by: Joseph Sweet ]
+Pie Number of slices to send: Send
This question is about assignment conversions, which do not require explicit casts. This example focuses on widening reference conversions. The JLS says, "A widening reference conversion exists from any type S to any type T, provided S is a subtype (�4.10) of T."

In this example:
  • The type of the reference "nc" is NewClass.
  • The type of the reference "o1" is Object.
  • The type of the reference "o2" is Interface.
  • The type of the reference "bc" is BaseClass.
  • The question is which of the variables (nc, o1, or o2) can we assign bc to without an explicit cast? In order to be a widening reference conversion, this means that bc must be a subtype of one of these.
    +Pie Number of slices to send: Send
    Thank you

    Now I got it. So the correct answer is (2).
    This is also what I got when running the code.
    All the other options gave me a compilation error: incompatible types
    [ July 31, 2007: Message edited by: Joseph Sweet ]
    +Pie Number of slices to send: Send
    Thats nice explanation marc.

    Adding to what marc had said, just giving out a detailed explanation on the assignment.

    Let's have a look at the classes or interfaces alone here.



    The inheritance hierarchy becomes as follows



    Now, consider your assignment options along with the objects you have created



    Case 1:

    nc=bc;

    Trying to assign the object of type "BaseClass" to an object of type "NewClass".

    As it is in the reverse direction of inheritance, it is NOT allowed!.

    Case 2:

    o1=bc;

    Trying to assign an object of type "BaseClass" to an object of type "Object" ("O1").

    Since the "BaseClass" does indirectly extend the "Object" class, it is allowed.

    Case 3:

    o2=bc;

    Before looking on the assignment, lets see what objects can the instance variable "o2" hold. As "o2" is of reference type "Interface I", it can ONLY hold the objects of class implementing the interface.

    In case 3, you are trying to assign an object of type "BaseClass" to an object of type implementing the interface "I'. As "BaseClass" does NOT implement the Interface "I", its NOT allowed.

    Getting back to your question asking which are allowed to compile without an error, only Option 2 wins the race!

    Hope this clears.
    [ July 31, 2007: Message edited by: Raghavan Muthu ]
    Oh the stink of it! Smell my tiny ad!
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 1027 times.
    Similar Threads
    String concatenation
    swapping object thru a method
    implementing and extending
    multiple casting
    interface
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 29, 2024 03:26:57.