• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics problem

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
ujjawal rohra
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it helped definitely

Thanks
 
ujjawal rohra
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ujjawal rohra
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Munee,

Thanks mate. Good luck to you as well.

Hopefully we all complete this journey of SCJP soon.

 
ujjawal rohra
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Prithvi,

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

Cheers Munees

My Blog
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Munee,

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

Thanks,
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Muneeswaran, please check your private messages.
 
Muneeswaran Balasubramanian
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prithvi,
Thanks for your sharing.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
You got style baby! More than this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic