Noam Wolf

Ranch Hand
+ Follow
since Jan 12, 2008
Merit badge: grant badges
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Noam Wolf

@Russel - the ArrayStoreException happens for the non-generic example... try it out.
well done. So does this mean you understand now?
Consider the following:



You will get a runtime ArrayStoreException on line 2. This is pretty similar to what you're trying to do with generics only with generics the compiler will catch the exception in compile time and will not let you do it.

If you change line 2 to:


You will no longer get a runtime error. Same thing for if you try to add A to your ArrayList. The reason, i assume and I hope people will correct me if i'm wrong, is that the underlying type of the ArrayList is a A and that's the only type you can add to the list because "?" is a runtime placeholder so basically Java can't know what you'll put in there but it will know that any thing that comes out of the list when you "get" it will be an A.

HTH
Hi Gylph,

I highly recommend you get an IDE that supports debugging. Write this program and step through execution while watching the variable "a". That will give you a real good idea of what's going on.
One hint that helped me a lot was the following:

Reference type determines the overloads.

Object type determines overrides.

So in your example you're defining "duplicate" in Shape and in its subclass "Circle". Since this is an override (it can't be an overload because you have not changed the signature) the compiler will know which class to call which means there is no ambiguity here so it will compile correctly.

Quiz time... where will the call for the following examples come from (Shape or Circle):



If you get this example you're golden.

HTH
Taken directly from my own notes that I used to prepare for SCJP 5.0:

1. The type of the variable declaration must match the type you pass to the actual object type.
List<Foo> f = new ArrayList<Foo>(); //OK
List<Foo> j = new ArrayList<Bar>(); //NOT OK even if Bar is-a Foo
Parent[] myArray = new Child[4]; //OK when Child is-a Parent

2. Method myMethod(List<Animal> list) can not accept a List<Dog>, List<Cat> ONLY List<Animal>.
List<Animal> list = new ArrayList<Animal>();
list.add(new Dog()); //OK when Dog is-a Animal
list.add(new Cat()); //OK when cat is-a Animal

3. Method myMethod(List<? extends Animal> list) CAN accept a List<Dog> ONLY if you do NOT ADD to the list
List<Dog> listDog = new ArrayList<Dog>();
myMethod(listDog); //OK only if myMethod doesn't add to the list.

4. <? extends ...> works for both classes and interfaces, you use extends for both.

5. <? super Dog> - Accept anything of type Dog or the SUPERTYPE of Dog, then you can add to this list.

6. <?> means you can pass ANY type (Dog, Cat, Car, Bag) but you CAN NOT add to the collection
<Object> means you can ONLY pass an Object type, but you CAN add to this collection
7. <?> = <? extends Object>, in both cases you can't add to the collection anyway

8. List<?> foo = new ArrayList<? extends Animal>(); // NO GOOD!!! WILL NOT COMPILE, can't use wildcard on right hand side

HTH
Hi Keith, can you please provide the question and possible answers? I don't have a copy of the book handy.
Write code. That's it. Read chapter 7 again and write code so that you understand the concepts. The key points are the inheritance order... if you can remember that Set's inherit collection then you'll remember there's and add method, and if you remember that TreeSet inherit's from SortedSet you'll remember that it's sorted and so on.

Focus on the questions at the end of the chapter, know them well. Understand the difference between <?>, <? extends ..>, and <? super ..>.

Keep reading and practicing and you'll be fine.
You're under a pretty strict deadline. Stick to K&B, try to cover all the chapters and do as many mock exams as possible... it won't be easy but it's doable. If you cover K&B and understand it completely you will be fine.
Hi Janardan,

Check this thread out: https://coderanch.com/t/268612/java-programmer-SCJP/certification/SCJP-Chapter

Looks like you have an outdated copy of the book...
Hi sumaraghavi,

First of all I believe you're supposed to quote your sources (I believe this is from K&B).

Second the value of x is incremented twice in the for loop IF ++x>2 or ++y>2, once AFTER the "if" condition has executed and then once more after x++ has executed.

So let's look at the results for x, y, and z after each iteration of the for loop.

iteration z x x++ y
1 0 1 (1 not >2) no call 1
2 1 2 (2 not >2) no call 2
3 2 3 (3 is >2) 4 (call) 2 (no change)
4 3 5 (5 is >2) 6 (call) 2 (no change)
5 4 7 (7 is >2) 8 (call) 2 (no change)
6 5 - not less than 5 for loop ends.

I think what you're missing here is that the value of x does NOT get incremented twice for the first 2 iterations because both cases of the if block are not true and therefore x++ is not reached.

I hope that helps.
Just another minor addition to remember for the exam is that if the super class has any Static init blocks or any instance init blocks then those would be called before the y += 3 statement.
Just one minor addition to what jaspreet wrote, if your implementing class is an Abstract class you do NOT have to implement any of the contract methods from the interface (you may implement them and you may define the as abstract, but you do not HAVE to implement them).

The first concrete class MUST implement any unimplemented method from the interface AND any abstract method from the abstract class.
Here's what ramesh said... but in a way I understand it.. hope it helps:

2 is compiling because that construction is perfectly ok. The method
accepts a List of anything effectively.

1 is not compiling because you are saying param1 should be of type
List<X> where X is a runtime type (the ? is a placeholder which is
filled in by X at runtime). Param2 is an object. Since the compiler
cannot confirm that argument2 (of type object) is of type X, it breaks
compilation.

Answer was given to me by Uber Java Engineer @ google...