File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics and overriding

 
Alex Serna
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alex Serna
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well after type erasure, both the methods are the same, still at compile time the methods are different. Read this...
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alex Serna
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic