wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding and Generics Isse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding and Generics Isse" Watch "Overriding and Generics Isse" New topic
Author

Overriding and Generics Isse

garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
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

Joined: Sep 22, 2008
Posts: 60
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

Joined: Aug 23, 2007
Posts: 124
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?


SCJP 88% | SCWCD 84%
Maleen Abeydeera
Greenhorn

Joined: Nov 10, 2008
Posts: 20
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

Joined: Nov 04, 2008
Posts: 69
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?


SCJP 5.0
garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
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

Joined: Jan 16, 2008
Posts: 110
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.


Nikos' Java blog
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Overriding and Generics Isse
 
Similar Threads
What's this code in Generics?
Generics and overriding
Need help in generics
Question in Generics -2
Generics