Noam Wolf

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

Recent posts by Noam Wolf

read chapter 7 of K&B, do the sample questions and actually code the examples and you'll be fine.
Hi Mukhan,

It looks like you are confusing overloading and overriding. Remember the main rules:

OVERRIDING - Must have same argument list and return type (or covariant return)

OVERLOADING - Reusing a method name but with different arguments which may have different return types if argument lists are also different

So now that we've established that, let's take another look at your first example:

What do you think that is? It's on OVERLOAD, same name and return type BUT different parameters.

So, in this case you are calling eat() twice, once with an Animal and once with a Horse BUT they are BOTH an Animal Reference (the stuff on the left of the assignment) and the rule for overloading is the the reference type determines what gets called... so if you actually try to run your code you'll see that it prints "Animal is eating!!" TWICE!! where did you get your result from?

Now the second example is an OVERRIDE because you define the eat() method in Animal and then Horse OVERRIDES this method (same arguments and return type) and the rules of overriding are determined by the Object type (the right side of the assignment) so will print "Horse is eating" BUT eat(string) is an OVERLOAD and it is determined by the reference type, which in your case is Animal... and since Animal doesn't have that overload the compiler will complain. If you change:


your code will compile.

Hope that helps
Hi Manny.

For future reference please use the "[CODE]" tags to post code because it's very hard to read your code. Also please state the question before and/or after the code and not *in* the code.

It looks like you have 2 questions, but before I get into those let me give you a slight overview on what it looks like you're trying to do.

You have a Dog class which implements the "equals" and the "hashCode" methods. As you know when you use a hashMap or any other hash based collection the objects you "put" in the collection are placed in "buckets" that are keyed on the objects hashCode. That hashCode is then used to quickly locate the "bucket" and then search for those objects from the collection and it is important to use a unique hashCode that will uniquely identify the object you're looking for.

What i found strange about your example code is that you're only inserting one object (dog) into your hashMap, had you tried inserting more than that you would have noticed how bad Dog's implementation of hashCode and equals were. So if a bucket contains more than one object, say you had two dog objects that had the same name for example, then the equals method is used to find the object you are searching for in that bucket.

The strings "==" operator just maps to it's "equals" method which doesn't rely on the hashCode method to verify equality.

hope that helps.
The method *should* be declared that way, in fact in most cases you want to create an interface to your method that allows any sub type that would work in it to be past (and therefore pass the top most interface/class in the inheritance hierarchy). If you limited getLongWords to accept a generic list alone you wouldn't be able to pass in any Set implementation, so you'd have to write a different method to handle Sets which also implement the Collection Interface.

Hope that helps!
I would like to know what you feel about the size of a team in a scrum environment. What should be the ideal size of a team? According to Ken Schwaber it should be roughly up to 7 people... but what happens when you get cross functional "horizontal" assets in a sprint and your teams grow to 14? how do you mitigate that?
Hi Gitesh,

In general compile time errors means your application/.java file won't compile, so you won't be able to run it and therefore won't get any runtime errors. A compile time error usually consists of syntax errors, or importing (or lack there of) of packages and such.

String s = "wow";
s.delete(1,2); //won't compile because delete doesn't exist in String

Runtime errors are errors that can not be caught in compile time, like indexOutOfBounds, division by zero, etc. These errors typically occur because of certain end cases that are not handled.

System.out.println(args[8]); //this could throw an IndexOutOfBoundException

I hope that helps...
it runs first only when you call it:

new Animal2("Louis") will run the arg-constructor first
new Animal2() will run the non-args constructor first


I'm not 100% sure i understand your question. When you use the "new" keyword you are instantiating a new instance of an object. So //1 & //2 are different instances.

With regards to the lock on dostuff... first of all your class doesn't compile (pubic == public) second you must implement the run() method (not the Run() method) and third you are not try to access do stuff in this program. To do so you need to do one of 2 things:

1. Call the run() method from the class, this will cause it to be a regular method call and no new threads will spawn.
2. You can wrap your class with a Thread object and call the start() method on that object (see Sun guide), this will span 2 separate threads that will call dostuff synchronously (because you used the synchronized key word on the method) so there won't be a deadlock or a race condition....

hope that helps...
Hi rahul,

I believe that you are accessing the method using an instance of class Test therefore it should be fine... think about this: how would you be able to run any program if you couldn't access non-static methods from main?
Well I agree with you guys but it raises a different question in my mind... which one of the answers are wrong?

Notice that solution E states:
Has-a relationships are always defined with instance variables.

Can a "Has-a" relationships be defined with a static variable?

If class Animal has a static variable of type Collar, and class Dog extends Animal, wouldn't class Dog "has-a" Collar too?

I'm confused regarding the working here... is a static variable considered an instance variable? I don't think so... sun reference but now i'm not quite sure....
So it seems the logic is to compare the first letter of the compared strings and return a positive number if the s2 starts with a "higher" (in the alphabet) letter, a negative number if s1 starts with a "higher" letter, and 0 if they are they same.... right?
update: I think i figured it out.... since the cstr calls for a var-args argument, namely int..., it's basically saying you can pass ZERO or more integers into the cstr... that's why this works!

//nailed it.

Hi, I'm a little stumped by this one too.

According to SCJP 5.0 book by Kathy and Bert. ("the book") on page 129/130 (as mentioned):

If you want a no-arg cstr and you've typed any other cstr(s) into your class code, the compiler won't provide the no-arg cstr (or any other cstr) for you. In other words, if you've typed in a cstr with arguments, you won't have a no-arg cstr unless you typed it in yourself!

That's pretty clear if you ask me. Now on the mock exam the following code appears (it compiles just fine and runs) but why??

Here is the code:

[ July 29, 2007: Message edited by: Noam Wolf ]