• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding and Generics Isse

 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Source: http://nikojava.wordpress.com/2008/10/09/scjp-mock-exam-for-generics/




What will be the output of the above two programs. Please explain.

Will these type of questions be there in the real exam. Please suggest as I have not studied overriding/overloading rules with respect to generics.
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first problem what will be the output if this code is compiled and executed:

and second prob is whether the code will compile or not.
 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the first part of the question, you have a parent reference variable
holding a child object, then calling a method defined in the Parent class
and overridden in the child. Selection of overridden methods is according
to the type of object actually held in the reference variable (runtime
type). Since the parent reference variable actually holds a child object,
the child's version of the method runs. And it prints out the proper message as it's legal to pass a generic List to a non-generic List.

As for the second part of the question, I think it compiles. why don't you
try compiling it?
 
Maleen Abeydeera
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ken,
for the first part, c is a parent a reference. So the compiler is trying to access the say() of Parent, and since the types don't match it gives an error, Isn't it?
[ November 10, 2008: Message edited by: Maleen Abeydeera ]
 
Henry Zhi Lin
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Truitt:
As for the second part of the question, I think it compiles. why don't you
try compiling it?


Are you sure the second part Compiles?
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had tried to compile it. It does not compile. Actually, I want to know whether such questions will be asked and if so, could anyone please send me the link of related tutorials as questions related to overriding/overloading with respect to generics are not there in KS & BB.
 
Nikos Pougounias
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by garima jain:
Will these type of questions be there in the real exam.


Hi Garim, this question about Generics is not in the real exam, because it's not in Kathy Sierra and Bert Bates' book.

However, I consider this knowledge useful for the real world.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic