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
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 ]
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 ]
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.
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 ]
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 ]
Post by:autobot
Oh the stink of it! Smell my tiny ad!
a bit of art, as a gift, the permaculture playing cards