Wyn Riley

Greenhorn
+ Follow
since Jun 18, 2007
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Wyn Riley

Hi

It says on page 24 that

"Methods and instance (nonlocal) variables are collectively known as members"

When it says "Methods" does it mean all methods, static and Instance, or just Instance methods?

Thanks

Wyn
K & B say

"Polymorphic method invocations apply only to overridden instance methods." (last line P 153 in Ch. 2 Two Minute Drill).

My university notes say

"It is common for instances of unrelated classes to have the same message in their protocols, and instances of those classes may or may not respond differently to that message. Such messages are termed polymorphic messages"

Who is correct?

Thanks

W
Hi Joseph, there's a lot of us finding this hard who, despite the hard work, do it because we enjoy it.

You will get there, good luck.

Wyn
Sorry, haven't quite got the hang of this code tag thing yet.
Hi

Just incase you got caught like I did.

While the version of the code below in the book is correct in that both versions of doStuff have a capital S, the version on the CD (which I copied and puzzled over until Marc Weber kindly pointed out the problem) has one of the methods called doStuff and the other called dostuff.

Some might say serves me right for copying.

Thanks Marc

I'll try and get past the beginner's mistakes as quickly as possible!
Sorry, that should have read

P 147 has an example of redefining a static method as follows
Hi

My first post, the first of many.

P 147 has an example of referencing as follows

class Animal {
static void doStuff() {
System.out.print("a ");
}
}

class Dog extends Animal {
static void dostuff() { // it's a redefinition,
// not an override
System.out.print("d ");
}

public static void main(String [] args) {
Animal [] a = {new Animal(), new Dog(), new Animal()};
for(int x = 0; x < a.length; x++)
a[x].doStuff(); // invoke the static method
}
}

which results in

a a a

I don't understand why there isn't a b in there, I'm guessing it's because the reference type is Animal?

Then I tried this, a set of dogs calling a static method

Set<Dog> ds = new HashSet<Dog>();

ds.add(new Dog());
ds.add(new Dog());
ds.add(new Dog());

for (Dog oneDS : ds)
{
oneDS.doStuff();
}

and still get

a a a

Please explain.
Hi Gopal

If you override equals so that 2 objects appear equal but they have different hashcodes then when you put them in a Set Collection (which should never contain duplicates) they may both get put in.

This is because the Set uses the hashcode to put the object in a 'bucket'. It then uses the hashcode of another object to check if an object like that is already in the set.

But if your 2 objects, which your equals() says are the same, end up in different buckets, then the set will wrongly contain them both.

So when changing equals() you have to change hashcode so that two equal objects have the same hashcode.

Hope that helps

Wyn