| Author |
Polymorphism?
|
Joseph Sweet
Ranch Hand
Joined: Jan 29, 2005
Posts: 327
|
|
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 ]
|
We must know, we will know. -- David Hilbert
|
 |
Jitendra Jha
Ranch Hand
Joined: Jan 28, 2007
Posts: 91
|
|
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
SCJP1.5
SCWCD1.5
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
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 ]
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Joseph Sweet
Ranch Hand
Joined: Jan 29, 2005
Posts: 327
|
|
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 ]
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
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.
|
 |
Joseph Sweet
Ranch Hand
Joined: Jan 29, 2005
Posts: 327
|
|
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 ]
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
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 ]
|
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
|
 |
 |
|
|
subject: Polymorphism?
|
|
|