aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics problem" Watch "Generics problem" New topic
Author

Generics problem

ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105


Why can not we add A to a list that accepts super type of B when A is a super type
What objects in the hierarchy can we add in go()....Please explain


SCJP 6
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

"List<? super B> la" means "la" can be used to hold reference to any list of class B or super class of b. But still you can only add the objects of b or subclass of b.
This is just for type safety. If we are allowed to add superclass of B then it can break the type safety rules. For example suppose you have below interfaces and class.

and a list

As per your opinion if i would be able to add a super class of ab then i can add A or B. So can not be sure whether the values in the list are A, B or ab but the whole idea of generics is to make sure the type.

But in actual i can add ab or any subclass of ab. In this case i will be sure that the class will be definitely ab as any class which is subclass of ab IS A ab.

Hope this will help
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hi Ujjwal,

It just means that the list can point towards any reference of super-type but addition is only allowed for
object of class B or sub-classes of B.

I am getting lazy eh!

Hope this helps,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
Yes it helped definitely

Thanks
ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105

The method go() declares that it can accept a list with Number and its super types
and when i am passing a list which can also accept a Number then what is the problem..Please explain
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear Ujjwal,


The problem comes here. You are saying create a list which can hold any subtype of number.

? extends Number

Your method signature says, only super-type of number or number. But the declaration says you can say something like this


Means you are creating a list which will hold Number also, and sub-type also and your method signature is saying, pass me
only Number or super-type of Number, so compiler is not taking any risk and restricting you there, so you don't pass any
sub-type of Number and screw the method.

Does it make sense?

Happy Preparation,

ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
Dear Prithvi
you have an answer to every question you must give scjp exam tomorrow itself why wait for may 6 yar

By the way i read you article "Making my own success story " and was motivated because i am also passing through a similar phase where i see people with me infront of me..But the problem is that the motivation is lost after a few hours and i again fall into depression..
rushikesh sawant
Ranch Hand

Joined: Dec 22, 2009
Posts: 65
ujjawal, your method definition says that it can accept Number type or its super types only.

but the list you are passing to that method says that it can accept Number type or its any of its subtypes also. So as generic is compile time protection compiler cannot make sure that you will pass only Number type to that method, hence compiler error.


SCJP 5.0 100%
ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105


Ok but now the method signature says that it can accept B or its super types (A also)
and the list says that it can contain A or its sub types(Both A and B) then why does this give error
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear Ujjawal,

Mate, i don't feel prepared yet, definitely will appear on 6th May.

Thank you very much for reading my post. You know your quality? I will tell you, you ask questions and that is the biggest thing.
That you ask. Well, motivation is like a habit, i mean you start with motivation but to persist is a habit, which i am trying to practice
even.

When are you appearing for the exam? Have you got any schedule? Let me know, i am curious to know.

Cheers,
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear Ujjawal,



It's the same reason. You are saying, you can store any sub-type of A, which can be some class named C also,
but your method signature says, please pass me a super-type of B or B itself.

Lets say you made a list of of some class C which extended A. It will be sibling to B, not super-type of B or type B.
Does it make sense now?

Cheers,

Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Prithivi,
It just means that the list can point towards any reference of super-type but addition is only allowed for
object of class B or sub-classes of B.


So the list can contain only the reference of class B or subclasses of B.
Am i right?

If i am wrong,please guide me...

Cheers Munees

My Blog


Cheers Munees
My Blog
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hi Munees,

The list reference passed to the method can be any super type of B or B itself but the objects
that can be added to the collection can only be either B or sub-class of B.

Hope this helps,
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Prithivi,
Thanks for your reply.I can understand clearly.

Best of luck for your SCJP Exam.


Cheers Munees

My Blog
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear Munee,

Thanks mate. Good luck to you as well.

Hopefully we all complete this journey of SCJP soon.

ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
Hii Prithvi

I havent got any schedule ..When i give a mock i catch my weakness on
a topic then start practising on it.Then again the same thing happens.

Additionally i am unable to concentrate for some days
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Prithivi,
I have a bit more doubt on this.

public List go(List<? super B> la


What is the use of this reference?

Prithvi wrote
The list reference passed to the method can be any super type of B or B itself but the objects
that can be added to the collection can only be either B or sub-class of B.

Then why we pass the reference as <? super B>.Why don't we pass reference as<B>?


If it is silly don't mistake me.

Cheers Munees

My Blog
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear Munee



If you are talking about the method signature, then it's generics way to let you add something
into a collection in a safe-way. On other ways around, you can treat the passed collection only
as read-only.

Hope this helps,
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
ujjawal rohra wrote:Hii Prithvi

I havent got any schedule ..When i give a mock i catch my weakness on
a topic then start practising on it.Then again the same thing happens.

Additionally i am unable to concentrate for some days


Dear Ujjawal,

Without a specific deadline and without a proper schedule, it would be difficult to achieve your target.
I will advice you to just have a look at K&B from beginning once more and then start with the mock
exams.

Why you are unable to concentrate? Are you running low in confidence right now regarding SCJP?

Best Wishes,
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Pritihivi,

Prithvi wrote
If you are talking about the method signature, then it's generics way to let you add something
into a collection in a safe-way.


Ya.I aggree with you.But if we pass <B> instead of <? super B> also a safe-way. Am i right or not?


If i am right, then why we go for <? super B>?

Cheers Munees

My Blog

Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear Munee,

You are correct but one down-side of the option you provided, here you can only and only
pass List of type B.


This is a polymorphic way. For suppose, you had a class B which had a bunch of sub-classes
overriding the method inherited from super-class of B. If you have to call that method which
is overriden in many sub-classes, then the signature you provided, you will have to overload
the method go() with all possible sub-types of B, which will prove to be quiet ugly. But in other
way, pass an sup-type reference and add anything like B or sub-types of B, and you will get
the polymorphic behavior.

Does that make sense?

Best Regards,
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Dear Prithvi,

Thanks for your reply.It make quite sense to me.

Cheers Munees

My Blog
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear Munee,

You are welcome. I am just about to finish compiling Generics notes, will upload them.

Thanks,
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3166
    
  47

Muneeswaran, please check your private messages.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Prithvi,
Thanks for your sharing.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear Munee,

You are welcome. I have updated section for Generics and Collection, will complete them today a bit of topics
are left. I have placed a link to view them.

Thanks,
 
 
subject: Generics problem