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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question from nikos blog(generics)" Watch "question from nikos blog(generics)" New topic
Author

question from nikos blog(generics)

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Will this code compile successfully?
1.
answer Yes, without warnings.
here why generic code + non-generic code is not generating warning?

2.
code 2 generates warning
o.k. because generic code + non-generic code is used.

SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

For the first one, both are implicitly same! So no warnings!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Sir,one more question from same source

here as method say() is overrided,so object type determines which method will invoked,so as child method is non generic it can hold anything(i.e. not primitive).
this code gives compiler error, why so?

Also if we put at line 16 it invokes child class say() method.
output:child
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

If I ask, Can you assign like this?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

And for the output as child, generics only for compiler! Do you got the point, why do these methods override even though the differ the method signature?
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Abimaran Kugathasan wrote:If I ask, Can you assign like this?

no , parameter type should have to be same on the both sides until and unless we are using wildcards(?).
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Arjun Srivastava wrote:
no , parameter type should have to be same on the both sides until and unless we are using wildcards(?).


So, that's why the compiler gives an Error!
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

but as overriding is happening, so it should consider the non-generic method which can take anything
but why it is considering generic method here of parent class just because of parent reference type?
but finally it invokes child method if we use line below
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

When compile time, it check the reference type, and at the time, generics also there. But at runtime, the generic and non-generic versions are same, VMI is there! {Generics only for compiler}
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Abimaran Kugathasan wrote:When compile time, it check the reference type, and at the time, generics also there. But at runtime, the generic and non-generic versions are same, VMI is there! {Generics only for compiler}

thanks, it is more clear now.
wonderful explanation
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

You are Welcome, If you want to check more, decompile the class file and check the methods! You'll get it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question from nikos blog(generics)