Rahul Choudhary

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

Recent posts by Rahul Choudhary

Thank you for well written examples Abhilash. That helped
Hey everyone. I was going through one of the FAQs in SCJP FAQ Page which was What are some potential trips/traps in the SCJP exam?

Two sentences I did not understand and I was hoping someone could explain them.

1. Forward references to variables gives a compiler error.
2. Primitive array types can never be assigned to each other, even though the primitives themselves can be assigned. For example, ArrayofLongPrimitives = ArrayofIntegerPrimitives gives compiler error even though longvar = intvar is perfectly valid.

Thanks.


The options are:
1. return e
2. return e2.getE()
3. return e2
4. return e.getE()

Option (1) and (2) compile fine. Option (3) and (4) give compile error.

1. Precisely, for e2 says,incompatible types. Found: GenericDemo<E>. Required: E.
My doubts is that, for e2, isn't E that is required, part of GenericDemo<E> which is e2's type ?

2. For return e.getE() says, incompatible types. Found: GenericDemo. Required: E. Here I'm first confused as to how the return type is determined for a method invocation. getE() according to method signature returns E. But invoking it through e.getE() returns GenericDemo?? How is that happening? And even variable e is of the type E? So where is GenericDemo coming from when compiler says found: GenericDemo?

3. And how is option(2) e2.getE() returning E that is actually compiled fine?

I hope I'm able to convey my doubts correctly.
Hey thanks for the heads up on attachment. And ya little silly mistakes! Thanks anyways.


Seems like a pretty straight forward program where in my knowledge I have followed all rules of Inner Class. Checked all {} and ; also. What could be the reason for the errors?

P.S. I wanted to show screenshot of the errors at command prompt but I don't see how you can upload from local drive.
If explanation is given for such concept in K&B please could you provide me with page number.
Hello Archna Singh, I happen to ask the same question a few days back. You'll find the answer in the second post of this thread:
Why it's possible to pass Non gen set to a method expecting Set<Number>
Hi ayush, dan and himai. From what all I have read in the posts from you guys is that if extends wild card is used it does not let you add anything. Not just <? extends Object> but something like <? extends Animal>. Also I have checked out other wild cards in K&B text.

  • <?>
  • <? extends XYZ>
  • <? super XYZ>
  • <Object>



  • Case1: <? extends XYZ> doesn't add anything from within method!


    Case 2: <?> doesn't add anything from within method!




    So can I safely understand it to be a rule that <? extends XYZ> and <?> as a method parameter won't allow anything to be added from within that method ass illustrated above?

    However, for <? super XYX> and <Object> it's even more peculiar.

    Case 3: <? super XYZ>


    it adds Dog object but not Animal object. It should right? In the program Dog extends Animal! A partial result.
    And if <? super Animal> is used, it adds both from within the method without any error! It shouldn't add Dog if <? super Animal> used right since Dog is subclass! What's the pattern or rule that I can understand here?

    Case 4: <Object>



    For the above program two things are happening. If I insert a type during instantiation (Dog or Animal) it gives compiler error saying "Object cannot be applied to Animal or Dog" and points to met(l1).
    But if I remove the type during instantiation, all works fine!

    Why this confusing thing is happening! I read up K&B but didn't find any such detailed explanation or if there I couldn't pinpoint Could I get help on each case please? Thanks again guys.
    However I have a similar doubt.


    The above program compiles and runs fine.
    But the program below give compiler error for every add() statement. Why? Isn't <? extends Object> refer to anything and everything that can be added to it?

    Thanks @ayush raj, very well explained


    On line 10 there is a type-safe set as a parameter for the method dostuff and it accepts non generic Set s1. How can it not flag an error for one of it's element being a string(s1.add("1"))? From what I understand, integer 0(s1.add(0)) is accepted since Integer IS-A Number but that's not the case for String so how come no error ? Sorry if it's a silly doubt.

    Since doStuff() is not overridden in Sample2 class the doStuff from Sample1 gets called


    Wouldn't it be an error that it's overridden but there is no definition in Sample2?


    The output is:
    Sample1 const
    Sample2 const
    Sample3 const
    sample1 dostuff

    My doubt is that super.doStuff() should invoke Sample2 method doStuff() if present. Why has it "propagated" upwards? I thought that happened only with no-arg super() being put implicitly (if not present) as first line of constructor.