nus-cs2030 / 2021-s2

2 stars 2 forks source link

Clarifications about Producer Extends Consumer Super (PECS) #79

Open gwajoon opened 3 years ago

gwajoon commented 3 years ago

I am rather unclear about the concept of PECS, and was trying to wrap my mind around it after my TA explained it to us during lab. From what I know, <? extends T> is used when you know that the wildcard needs to be a wrapper class that extends the type T. And I remember someone in my lab mentioning that it becomes read-only and you cannot add objects to the class. But I am still confused about the whole idea of it does anybody have a example that can help me visualise this better?

Specific Topics this question is related to (If Any)

Screenshot of the slide:

Screenshot 2021-03-17 at 9 02 37 PM
Keithlimhs commented 3 years ago

Hi, for List< ? extends T >, you cannot add objects to the class because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object you want to add is allowed in that list. The only "guarantee" is that you can read from it and you will get a T or a subclass of T. For example, let Integers and Doubles be a subclass of Numbers. List < ? extends Numbers > foo = new ArrayList< Numbers >(), List < ? extends Numbers > foo = new ArrayList< Integers >(), and List < ? extends Numbers > foo = new ArrayList< Doubles >() are all valid. However, when you want to write to foo, you cannot add an Integer because foo may be pointing to a List < Doubles >, you cannot add a Double because foo may be pointing to a List< Integers >, and you cannot add a Number because foo may be pointing to a List < Numbers >. Hence there is no guarantee of writing to the list

wentinggy commented 3 years ago

Hi gwajoon, I found this image online, hopefully it helps hehe

Screenshot 2021-03-18 at 7 38 51 PM
jettyevy commented 3 years ago

Hi, for List< ? extends T >, you cannot add objects to the class because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object you want to add is allowed in that list. The only "guarantee" is that you can read from it and you will get a T or a subclass of T. For example, let Integers and Doubles be a subclass of Numbers. List < ? extends Numbers > foo = new ArrayList< Numbers >(), List < ? extends Numbers > foo = new ArrayList< Integers >(), and List < ? extends Numbers > foo = new ArrayList< Doubles >() are all valid. However, when you want to write to foo, you cannot add an Integer because foo may be pointing to a List < Doubles >, you cannot add a Double because foo may be pointing to a List< Integers >, and you cannot add a Number because foo may be pointing to a List < Numbers >. Hence there is no guarantee of writing to the list

Thanks Keith, I initially had this problem in the first place, thanks for clarifying for me! This comment has been a godsend! xoxo

Keithlimhs commented 3 years ago

you're very much welcome! Do hit me up if you have further queries. Keep living it big

jettyevy commented 3 years ago

Yup I will! Looking forward to interacting with you in the future if I have any more questions!

you're very much welcome! Do hit me up if you have further queries. Keep living it big

brennanleez-coder commented 3 years ago

Hi, for List< ? extends T >, you cannot add objects to the class because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object you want to add is allowed in that list. The only "guarantee" is that you can read from it and you will get a T or a subclass of T. For example, let Integers and Doubles be a subclass of Numbers. List < ? extends Numbers > foo = new ArrayList< Numbers >(), List < ? extends Numbers > foo = new ArrayList< Integers >(), and List < ? extends Numbers > foo = new ArrayList< Doubles >() are all valid. However, when you want to write to foo, you cannot add an Integer because foo may be pointing to a List < Doubles >, you cannot add a Double because foo may be pointing to a List< Integers >, and you cannot add a Number because foo may be pointing to a List < Numbers >. Hence there is no guarantee of writing to the list

Thanks Keith, I initially had this problem in the first place, thanks for clarifying for me! This comment has been a godsend! xoxo

Hi jettyevy, could u explain it to me more in depth maybe over zoom? still cant really grasp this concept.

gwajoon commented 3 years ago

Wentinggy, how did you know I was a visual learner? That image helped me understand the concept a lot better at one glance! Keithlimhs, thank you for your enlightenment. I frequently see you enlightening other users here, please keep up the good work and spirit of peer-to-peer learning! You guys are really making the community grow together one step at a time :)

jettyevy commented 3 years ago

Hi, for List< ? extends T >, you cannot add objects to the class because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object you want to add is allowed in that list. The only "guarantee" is that you can read from it and you will get a T or a subclass of T. For example, let Integers and Doubles be a subclass of Numbers. List < ? extends Numbers > foo = new ArrayList< Numbers >(), List < ? extends Numbers > foo = new ArrayList< Integers >(), and List < ? extends Numbers > foo = new ArrayList< Doubles >() are all valid. However, when you want to write to foo, you cannot add an Integer because foo may be pointing to a List < Doubles >, you cannot add a Double because foo may be pointing to a List< Integers >, and you cannot add a Number because foo may be pointing to a List < Numbers >. Hence there is no guarantee of writing to the list

Thanks Keith, I initially had this problem in the first place, thanks for clarifying for me! This comment has been a godsend! xoxo

Hi jettyevy, could u explain it to me more in depth maybe over zoom? still cant really grasp this concept.

Yup! I will be doing a zoom session with Keithlimhs tonight! I'll pm you our zoom link!

gwajoon commented 3 years ago

Hi, for List< ? extends T >, you cannot add objects to the class because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object you want to add is allowed in that list. The only "guarantee" is that you can read from it and you will get a T or a subclass of T. For example, let Integers and Doubles be a subclass of Numbers. List < ? extends Numbers > foo = new ArrayList< Numbers >(), List < ? extends Numbers > foo = new ArrayList< Integers >(), and List < ? extends Numbers > foo = new ArrayList< Doubles >() are all valid. However, when you want to write to foo, you cannot add an Integer because foo may be pointing to a List < Doubles >, you cannot add a Double because foo may be pointing to a List< Integers >, and you cannot add a Number because foo may be pointing to a List < Numbers >. Hence there is no guarantee of writing to the list

Thanks Keith, I initially had this problem in the first place, thanks for clarifying for me! This comment has been a godsend! xoxo

Hi jettyevy, could u explain it to me more in depth maybe over zoom? still cant really grasp this concept.

Yup! I will be doing a zoom session with Keithlimhs tonight! I'll pm you our zoom link!

Can hit me up with the zoom link?

brennanleez-coder commented 3 years ago

Hi, for List< ? extends T >, you cannot add objects to the class because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object you want to add is allowed in that list. The only "guarantee" is that you can read from it and you will get a T or a subclass of T. For example, let Integers and Doubles be a subclass of Numbers. List < ? extends Numbers > foo = new ArrayList< Numbers >(), List < ? extends Numbers > foo = new ArrayList< Integers >(), and List < ? extends Numbers > foo = new ArrayList< Doubles >() are all valid. However, when you want to write to foo, you cannot add an Integer because foo may be pointing to a List < Doubles >, you cannot add a Double because foo may be pointing to a List< Integers >, and you cannot add a Number because foo may be pointing to a List < Numbers >. Hence there is no guarantee of writing to the list

Thanks Keith, I initially had this problem in the first place, thanks for clarifying for me! This comment has been a godsend! xoxo

Hi jettyevy, could u explain it to me more in depth maybe over zoom? still cant really grasp this concept.

Yup! I will be doing a zoom session with Keithlimhs tonight! I'll pm you our zoom link!

Can hit me up with the zoom link?

Hi gwajoon, jettyevy, keithlimhs, it was really nice connecting with all of you guys over zoom since classes took a huge online turn due to covid. Thank you for clarifying my misconceptions and I hope my analogies helped you, for example, i remember Function interface has a SAM called apply from this pneumonic, FIHASCA.