File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics and overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics and overriding" Watch "Generics and overriding" New topic
Author

Generics and overriding

Alex Serna
Ranch Hand

Joined: Sep 18, 2009
Posts: 58
Hi,

I don't understand why this doesn't compile. It seems that the child method won't override it's parent's, but why?



I got this from this great generics mock exam.

Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
Alex Serna wrote:Hi,

I don't understand why this doesn't compile. It seems that the child method won't override it's parent's, but why?



I got this from this great generics mock exam.



This looks like an overload, not an override.
The arguments for the methods are different, despite the fact you're usng Lists.
The Generic types are different, and thus they take different types of Lists.
I don't know why it wont complile.
You should also indicate the compiler error you get, and 'm sure someone in the forum can help.
HTH
Fritz


SCJP 6.0
Alex Serna
Ranch Hand

Joined: Sep 18, 2009
Posts: 58
This is the compiler error:
The method say(List<Boolean>) of type Child has the same erasure as say(List) of type Parent but does not override it Test.java


I might be wrong but think this shouldn't be an overload because for the compiler both are List<E>, aren't they?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Well after type erasure, both the methods are the same, still at compile time the methods are different. Read this...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18544
    
  40

Alex Serna wrote:
I might be wrong but think this shouldn't be an overload because for the compiler both are List<E>, aren't they?


If both methods did take a List<E>, then yes, it would be overriding, and not overloading. Like so...



And this compiles...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Alex Serna
Ranch Hand

Joined: Sep 18, 2009
Posts: 58
Thank you everyone!!!

One last thing.... is this more than needed for the exam??? Because I've finished K & B book and it feels that I never stop finding new stuff for the exam.

Regards,

Alex.
Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
Yes, there is a lot more to go after finishing the K&B book.

Do as many mock exams as you need to prepare for the exam.

Based on my experience: I'd say the finishing the K&B book will make you 40-60% ready for the exam, depending on how much programming background you have.

HTH
 
jQuery in Action, 2nd edition
 
subject: Generics and overriding
 
Similar Threads
Generics and Overriding
Overriding and Generics Isse
compile error in generics
output in generics
Question in Generics -2