permaculture playing cards
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A Generics and Collections Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A Generics and Collections Problem" Watch "A Generics and Collections Problem" New topic
Author

A Generics and Collections Problem

Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
hi all!
next is a Generics and Collections Problem from K&B book:

Which of the following occur at least once when the code is compiled and run?
(Choose all that apply.)
A. Adding Hans
B. Adding Lotte
C. Adding Jane
D. Total: 3
E. Total: 5
F. The code does not compile.
G. An exception is thrown at runtime.


the correct answer is F.and they explained:
F is correct. The problem here is in Group's add() method�it should have been add(Person), since the class extends HashSet<Person>. So this doesn't compile.

I don't understand above explaination.I think :
even if the class extends HashSet<Person>,the method :
public boolean add(Object o) {
System.out.println("Adding: " + o);
return super.add(o);
}
is also a overloading method.
so why this doesn't compile?


who dare win!<br />SCJP5(94%)|SCWCD5(86%)|SCBCD(100%)|SCEA in progress
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282

Notice the word object (capitalized), someone will be along in a moment and give more information about the <Person> syntax, but the add method requires what is called a cast.

In Collections, the cast is to an Object, so it's super.add((Object) o);. The call to super is not an overloading method, it is a call to the superclass implementation of the method. But since the code defines the add method, it is an override (not an overload): What the override here does is print out the value so that the operation of the program can been seen for purposes of the book.

It requires little change to get the code to compile. Just change return super.add(o); to return super.add((Object) o);


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
hi all!
the next code compiled and runned ok:

the output is:
Adding: Hans
Adding: Lotte
Adding: Jane
Adding: Hans
Adding: Jane
Total: 5


and the next code:

this code has a error,and the output is:
Total: 5

and the next code:

//the code does not compile!

I don't understand ,if i change "public boolean add(int o)" to "public boolean add(Object o)",the code does not compile!

thanks in advance!
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
hi Nicholas Jordan!
if i change return super.add(o); to return super.add((Object) o);
the code does not compile too!!
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Hi Fudong,

I believe Nicolas meant to say that you need to cast the superclass method parameter like this: "super.add((Person)o);"

The problem here is that HashSet<Person> only has an add(Person) method, not add(Object).


SCJP 5.0
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
Hi,Kelvin
why add(Object) is not a overloading method in Group class?as Group class has inherited a add(Person) method?
hope you help.
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
who can help me?thanks in advance1
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Jia Fudong:
Hi,Kelvin
why add(Object) is not a overloading method in Group class?as Group class has inherited a add(Person) method?
hope you help.


Ah, yes, that's due to a different problem which I overlooked. The problem I described earlier was solely regarding the "super.add(o)" line.

However, there's indeed another more fundamental compile-time error: Group's add() method cannot be declared with an add(Object) type. Here's the compiler message you'll probably get: "Name clash: The method add(Object) of type Group has the same erasure as add(E) of type HashSet<E> but does not override it."

As you should know from reading K&B, parameterized types don't actually exist at runtime. Consequently, at compile-time it appears that HashSet<Person>'s add() method takes a Person parameter--but at runtime the "Person" type parameter is erased, so add() actually takes an Object parameter! Therefore, Java doesn't allow you to declare a new add(Object) method, because this will conflict with the supposed add(Person) method, yet at the same time it doesn't override add(Person).

This is another unfortunate consequence of Java's type erasure system. Here are two other examples of the same issue:

The two methods above have duplicate signatures, because the parameterized type T becomes Object at runtime.

The two methods above also have duplicate signatures, because their parameter types both become just "List" at runtime.

Hope this helps clarify the problem! This is admittedly a tricky problem; I missed it myself when I first read your post.
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
hi Kelvin very ,
Thanks for the explaination my doubt got cleared.
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: A Generics and Collections Problem
 
jQuery in Action, 3rd edition